重新搞一波 复习巩固

简单记录 慕课网 imooc Java工程师

List Set Map

集合框架

疑问:

为什么使用集合,而不用数组?

我也不知道,那为什么要使用数组呢?

我们需要存储一组(类型相同)的元素的时候,使用数组来保存的。(数组具有固定的大小,存储数据类型是单一的。)

但数组一旦定义,长度不能再变化。想保存动态的数据那怎么办呢?那就需要集合框架了,集合大小可动态扩展,可根据需要改变集合的大小,是存放引用类型数据的容器 。

集合应用场景

  • 无法预测存储数据的数量

  • 同时存储具有一对一关系的数据

  • 需要进行数据的增删改查

  • 数据重复问题

Set不允许插入重复的元素

集合框架的体系结构

Collection -> List -> ArrayList、LinkedList

Collection -> Queue -> LinkedList

Collection -> Set -> HashSet

Map -> HashMap

List(列表)

  • List是元素有序并且可以重复的集合,称为序列

  • List可以精确的控制每个元素的插入位置,或删除某个位置的元素

  • List的两个主要实现类是ArrayList和LinkedList

ArrayList

  • ArrayList底层是由数组实现的

  • 动态增长,以满足应用程序的需求

  • 在列表尾部插入或删除非常有效

  • 更适合查找和更新元素

  • ArrayList中的元素可以为null

案例

案例:用ArrayList存储编程语言的名称,并输出。

名称包括“Java”、“C++”、“C“、”Go“、和”Swift“

ListDemo.java

package list;

import java.util.ArrayList;
import java.util.List; /**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-18 11:40 AM
*/ public class ListDemo { public static void main(String[] args) {
// 用ArrayList存储编程语言的名称,并输出
List list=new ArrayList();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Go");
list.add("Swift");
//输出列表中元素的个数
System.out.println("列表中元素的个数为:"+list.size()); //遍历输出所有编程语言
System.out.println("**************************************");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+",");
} //移除列表中的C++
System.out.println();
//list.remove(2);
list.remove("C++");
System.out.println("**************************************");
System.out.println("移除C++以后的列表元素为:");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+",");
}
} }

案例 公告管理

需求

  • 公告的添加和显示
  • 在指定位置处插入公告
  • 删除公告
  • 修改公告

公告类属性

  • 编号id
  • 标题title
  • 创建人creator
  • 创建时间createTime

公告类方法

  • 构造方法
  • 获取和设置属性值的方法

Notice.java

package list;
import java.util.Date; /**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-20 06:46 AM
*/
public class Notice {
private int id;//ID
private String title;//标题
private String creator;//创建人
private Date createTime;//创建时间
public Notice(int id, String title, String creator, Date createTime) {
super();
this.id = id;
this.title = title;
this.creator = creator;
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
} }

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=2602:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection ArrayList.ArrayListDemo
列表中元素的个数为:7
**************************************
Java,C,C++,Go,Swift,Python,JavaScript,
**************************************
移除C++以后的列表元素为:
Java,C,Go,Swift,Python,JavaScript,
Process finished with exit code 0

NoticeTest.java

package list;
import java.util.ArrayList;
import java.util.Date;
/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-21 11:47 AM
*/ public class NoticeTest { public static void main(String[] args) {
// 创建Notice类的对象,生成三条公告
Notice notice1 = new Notice(1, "欢迎来到慕课网!", "管理员", new Date());
Notice notice2 = new Notice(2, "请同学们按时提交作业!", "老师", new Date());
Notice notice3 = new Notice(3, "考勤通知!", "老师", new Date()); // 添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3); // 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} System.out.println("**************************************");
// 在第一条公告后面添加一条新公告
Notice notice4 = new Notice(4, "在线编辑器可以使用啦!", "管理员", new Date());
noticeList.add(1, notice4); // 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} System.out.println("**************************************");
// 删除按时提交作业的公告
noticeList.remove(2);
// 显示公告
System.out.println("删除公告后的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} //将第二条公告改为:Java在线编辑器可以使用啦!
System.out.println("**************************************");
//修改第二条公告中title的值
notice4.setTitle("Java在线编辑器可以使用啦!");
noticeList.set(1, notice4);
System.out.println("修改后公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
} } package ArrayList;
import java.util.ArrayList;
import java.util.Date;
/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-19 11:47 AM
*/ public class NoticeTest { public static void main(String[] args) {
// 创建Notice类的对象,生成三条公告
Notice notice1 = new Notice(1, "欢迎来到慕课网!", "管理员", new Date());
Notice notice2 = new Notice(2, "请同学们按时提交作业!", "老师", new Date());
Notice notice3 = new Notice(3, "考勤通知!", "老师", new Date()); // 添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3); // 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} System.out.println("**************************************");
// 在第一条公告后面添加一条新公告
Notice notice4 = new Notice(4, "在线编辑器可以使用啦!", "管理员", new Date());
noticeList.add(1, notice4); // 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} System.out.println("**************************************");
// 删除按时提交作业的公告
noticeList.remove(2);
// 显示公告
System.out.println("删除公告后的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
} //将第二条公告改为:Java在线编辑器可以使用啦!
System.out.println("**************************************");
//修改第二条公告中title的值
notice4.setTitle("Java在线编辑器可以使用啦!");
noticeList.set(1, notice4);
System.out.println("修改后公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
} }

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=1934:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection ArrayList.NoticeTest
公告的内容为:
1:欢迎来到慕课网!
2:请同学们按时提交作业!
3:考勤通知!
**************************************
公告的内容为:
1:欢迎来到慕课网!
2:在线编辑器可以使用啦!
3:请同学们按时提交作业!
4:考勤通知!
**************************************
删除公告后的内容为:
1:欢迎来到慕课网!
2:在线编辑器可以使用啦!
3:考勤通知!
**************************************
修改后公告的内容为:
1:欢迎来到慕课网!
2:Java在线编辑器可以使用啦!
3:考勤通知! Process finished with exit code 0

Set

Set是元素无序并且不可以重复的集合,被称为集。

HashSet

  • HashSet是Set的一个重要实现类,称为哈希集

  • HashSet中的元素无序并且可以重复

  • HashSet中只允许一个null元素

  • 具有良好的存取和查找性能

案例

  • 用HashSet存储多个表示颜色的英文单词,并输出

  • 单词包括:

    ”blue“、”red“、”black“、”yellow“、和”white“

WordDemo.java

package set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-20 12:02 PM
*/ public class WordDemo { public static void main(String[] args) {
// 将英文单词添加到HashSet中
Set set = new HashSet();
// 向集合中添加元素
set.add("blue");
set.add("red");
set.add("black");
set.add("yellow");
set.add("white");
// 显示集合的内容
System.out.println("集合中的元素为:");
Iterator it = set.iterator();
// 遍历迭代器并输出元素
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
// 在集合中插入一个新的单词
// set.add("green");
set.add("green");
it = set.iterator();
// 遍历迭代器并输出元素
System.out.println("**************************");
System.out.println("插入新元素后的输出结果为:");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
set.add("white");
it = set.iterator();
// 遍历迭代器并输出元素
System.out.println("**************************");
System.out.println("插入重复元素后的输出结果为:");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
//插入失败,但是不会报错
} }

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=4266:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection set.WordDemo
集合中的元素为:
red blue white black yellow
**************************
插入新元素后的输出结果为:
red green blue white black yellow
**************************
插入重复元素后的输出结果为:
red green blue white black yellow
Process finished with exit code 0

案例:宠物猫信息管理

需求

  • 添加和现实宠物猫信息
  • 查询某只宠物猫的信息并输出
  • 修改宠物猫的信息
  • 删除宠物猫信息

属性

  • 名字 name
  • 年龄 month
  • 品种 species

方法

  • 构造方法

  • 过去和设置属性值的方法

  • 其他方法

Cat.java

package set;

/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-20 12:07 PM
*/ public class Cat {
private String name; //名字
private int month; //年龄
private String species;//品种 //构造方法
public Cat(String name, int month, String species) {
super();
this.name = name;
this.month = month;
this.species = species;
}
//getter与setter方法
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getMonth() {
return month;
} public void setMonth(int month) {
this.month = month;
} public String getSpecies() {
return species;
} public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + month;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((species == null) ? 0 : species.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
//判断对象是否相等,相等则返回true,不用继续比较属性了
if(this==obj)
return true;
//判断obj是否是Cat类的对象
if(obj.getClass()==Cat.class){
Cat cat=(Cat)obj;
return cat.getName().equals(name)&&(cat.getMonth()==month)&&(cat.getSpecies().equals(species));
} return false;
} }

CatTest.java

package set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; /**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-20 13:08 PM
*/ public class CatTest { public static void main(String[] args) {
// 定义宠物猫对象
Cat huahua = new Cat("花花", 12, "英国短毛猫");
Cat fanfan = new Cat("凡凡", 3, "中华田园猫");
// 将宠物猫对象放入HashSet中
Set<Cat> set = new HashSet<Cat>();
set.add(huahua);
set.add(fanfan);
// 显示宠物猫信息
Iterator<Cat> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} // 再添加一个与花花属性一样的猫
Cat huahua01 = new Cat("花花", 12, "英国短毛猫");
set.add(huahua01);
System.out.println("**********************************");
System.out.println("添加重复数据后的宠物猫信息:");
it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} System.out.println("**********************************");
// 重新插入一个新宠物猫
Cat huahua02 = new Cat("花花二代", 2, "英国短毛猫");
set.add(huahua02);
System.out.println("添加花花二代后的宠物猫信息:");
it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} System.out.println("**********************************");
// 在集合中查找花花的信息并输出
if (set.contains(huahua)) {
System.out.println("花花找到了!");
System.out.println(huahua);
} else {
System.out.println("花花没找到!");
}
// 在集合中使用名字查找花花的信息
System.out.println("**********************************");
System.out.println("通过名字查找花花信息");
boolean flag = false;
Cat c = null;
it = set.iterator();
while (it.hasNext()) {
c = it.next();
if (c.getName().equals("花花")) {
flag = true;// 找到了
break;
}
}
if (flag) {
System.out.println("花花找到了");
System.out.println(c);
} else {
System.out.println("花花没找到");
} // 删除花花二代的信息并重新输出
for (Cat cat : set) {
if ("花花二代".equals(cat.getName())) {
set.remove(cat);
break; }
}
System.out.println("**********************************"); System.out.println("删除花花二代后的数据");
for(Cat cat:set){
System.out.println(cat);
}
//删除集合中的所有宠物猫信息
System.out.println("**********************************");
boolean flag1=set.removeAll(set);
if(set.isEmpty()){
System.out.println("猫都不见了。。。");
}else{
System.out.println("猫还在。。。");
}
}
}

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=4377:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection set.CatTest
[姓名:花花, 年龄:12, 品种:英国短毛猫]
[姓名:凡凡, 年龄:3, 品种:中华田园猫]
**********************************
添加重复数据后的宠物猫信息:
[姓名:花花, 年龄:12, 品种:英国短毛猫]
[姓名:凡凡, 年龄:3, 品种:中华田园猫]
**********************************
添加花花二代后的宠物猫信息:
[姓名:花花, 年龄:12, 品种:英国短毛猫]
[姓名:凡凡, 年龄:3, 品种:中华田园猫]
[姓名:花花二代, 年龄:2, 品种:英国短毛猫]
**********************************
花花找到了!
[姓名:花花, 年龄:12, 品种:英国短毛猫]
**********************************
通过名字查找花花信息
花花找到了
[姓名:花花, 年龄:12, 品种:英国短毛猫]
**********************************
删除花花二代后的数据
[姓名:花花, 年龄:12, 品种:英国短毛猫]
[姓名:凡凡, 年龄:3, 品种:中华田园猫]
**********************************
猫都不见了。。。 Process finished with exit code 0

iterator(迭代器)

  • Iterator接口可以以统一的方式对各种集合元素进行遍历

  • hasNext()方法检测集合中是否还有下一个元素

  • next()方法返回集合中的下一个元素

Map

  • Map中的数据是以键值对(key-value)的形式存储的
  • key-value以Entry类型的对象实例存在
  • 可以通过key值快速地查找value
  • 一个映射不能包含重复的键
  • 每个键最多只能映射到一个元值

HashMap

  • 基于哈希表的Map接口的实现
  • 允许使用null值和null键
  • key值不允许重复
  • HashMap中的Entry对象是无序排列的

案例

完成一个类似字典的功能

  • 将单词以及单词中的注释存储到HashMap中
  • 显示HashMap中的内容
  • 查找某个单词的注释并显示

DictionaryDemo.java

package Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.*; /**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-22 12:18 PM
*/ public class DictionaryDemo { public static void main(String[] args) {
Map<String,String> animal=new HashMap<String,String>();
System.out.println("请输入三组单词对应的注释,并存放到HashMap中");
Scanner console=new Scanner(System.in);
//添加数据
int i=0;
while(i<3){
System.out.println("请输入key值(单词):");
String key=console.next();
System.out.println("请输入value值(注释):");
String value=console.next();
animal.put(key, value);
i++;
}
//打印输出value的值(直接使用迭代器)
System.out.println("*****************************************");
System.out.println("使用迭代器输出所有的value:");
Iterator<String> it=animal.values().iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("*****************************************");
//打印输出key和value的值
//通过entrySet方法
System.out.println("通过entrySet方法得到key-value:");
Set<Entry<String, String>> entrySet=animal.entrySet();
for(Entry<String, String> entry:entrySet){
System.out.print(entry.getKey()+"-");;
System.out.println(entry.getValue());
}
System.out.println();
System.out.println("*****************************************"); //通过单词找到注释并输出
//使用keySet方法
System.out.println("请输入要查找的单词:");
String strSearch=console.next();
//1.取得keySet
Set<String> keySet=animal.keySet();
//2.遍历keySet
for(String key:keySet){
if(strSearch.equals(key)){
System.out.println("找到了!"+"键值对为:"+key+"-"+animal.get(key));
break;
}
}
} }

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=5621:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection Map.DictionaryDemo
请输入三组单词对应的注释,并存放到HashMap中
请输入key值(单词):
hello
请输入value值(注释):
你好
请输入key值(单词):
world
请输入value值(注释):
世界
请输入key值(单词):
Java
请输入value值(注释):
Java就是Java咯
*****************************************
使用迭代器输出所有的value:
Java就是Java咯 世界 你好
*****************************************
通过entrySet方法得到key-value:
Java-Java就是Java咯
world-世界
hello-你好 *****************************************
请输入要查找的单词:
Java
找到了!键值对为:Java-Java就是Java咯 Process finished with exit code 0

商品信息管理

使用HashMap对商品信息进行管理

  • 其中key为商品编号,value为商品对象

对HashMap中的商品信息进行增、删、改、查操作

分析商品信息类

属性

  • 商品编号:id

  • 商品名称:name

  • 商品价格:price

方法

  • 构造方法
  • 获取和设置属性值的方法
  • 其他方法

Goods.java

package map;

/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-21 12:51 PM
*/ public class Goods {
private String id;//商品编号
private String name;//商品名称
private double price;//商品价格
//构造方法
public Goods(String id,String name,double price){
this.id=id;
this.name=name;
this.price=price;
} //getter和setter方法
public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
}
public String toString(){
return "商品编号:"+id+",商品名称:"+name+",商品价格:"+price;
}
}

GoodsTest.java

package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner; /**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2018-12-21 12:52 PM
*/ public class GoodsTest { public static void main(String[] args) { Scanner console = new Scanner(System.in);
// 定义HashMap对象
Map<String, Goods> goodsMap = new HashMap<String, Goods>();
System.out.println("请输入三条商品信息:");
int i = 0;
while (i < 3) {
System.out.println("请输入第" + (i + 1) + "条商品信息:");
System.out.println("请输入商品编号:");
String goodsId = console.next();
// 判断商品编号id是否存在
if (goodsMap.containsKey(goodsId)) {
System.out.println("该商品编号已经存在!请重新输入!");
continue;
}
System.out.println("请输入商品名称:");
String goodsName = console.next();
System.out.println("请输入商品价格:");
double goodsPrice = 0;
try {
goodsPrice = console.nextDouble();
} catch (java.util.InputMismatchException e) {
System.out.println("商品价格的格式不正确,请输入数值型数据!");
console.next();
continue;
}
Goods goods = new Goods(goodsId, goodsName, goodsPrice);
// 将商品信息添加到HashMap中
goodsMap.put(goodsId, goods);
i++;
}
// 遍历Map,输出商品信息
System.out.println("商品的全部信息为:");
Iterator<Goods> itGoods = goodsMap.values().iterator();
while (itGoods.hasNext()) {
System.out.println(itGoods.next());
} } }

Result

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=8689:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\imooc_Java_Engineer\out\production\Collection map.GoodsTest
请输入三条商品信息:
请输入第1条商品信息:
请输入商品编号:
001
请输入商品名称:
不清楚
请输入商品价格:
100
请输入第2条商品信息:
请输入商品编号:
002
请输入商品名称:
不知道
请输入商品价格:
200
请输入第3条商品信息:
请输入商品编号:
你好呀
请输入商品名称:
300
请输入商品价格:
300
商品的全部信息为:
商品编号:001,商品名称:不清楚,商品价格:100.0
商品编号:002,商品名称:不知道,商品价格:200.0
商品编号:你好呀,商品名称:300,商品价格:300.0 Process finished with exit code 0

集合总结

List 、Map、Set总结

Collection Map

List Queue Set

ArrayList LinkedList HashSet

HashMap <Key,Value>

Java常用类的方法 查找去使用

ArrayList

  • 底层由数组实现
  • 元素有序且可以重复
  • 可以动态增长,以满足应用程序的需求
  • 元素值可以为null

顺序存储

常用方法的使用

HashSet

  • 元素无序并且不可以重复
  • 只允许一个null元素

HashMap

  • 键不能重复
  • 允许使用null值和null键
  • HashMap中的Entry对象是无序排列的

Iterator 迭代器

iterator接口以统一的方法对各种集合元素进行遍历

Iterator<String> it = set.iterator();
while(it.hasNext()){
System.out.print(it.next() + "");
}

下一个数据是否还有内容 it.hasNext()

泛型

hashCode()

public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0:name.hashCode());
result = prime * result + ((species == null) ? 0:name.hashCode());
return result;
}

equals()

public boolean equals(Object obj){
if(this == obj)
return true;
if(obj.getClass() == Cat.class){
Cat cat = (Cat) obj;
return cat.getName().equals(name) && (cat.getAge()==age) & cat.getSpecies().equals(species);
return false;
}
}

参考资料

参考资料

[1]-慕课网 Java工程师就业班 - Java 零基础入门-Java集合

【Java】集合框架(List Set Map)的更多相关文章

  1. Java集合框架List,Map,Set等全面介绍

    Java集合框架的基本接口/类层次结构: java.util.Collection [I]+--java.util.List [I]   +--java.util.ArrayList [C]   +- ...

  2. 【转】Java集合框架List,Map,Set等全面介绍

    原文网址:http://android.blog.51cto.com/268543/400557 Java Collections Framework是Java提供的对集合进行定义,操作,和管理的包含 ...

  3. 34、Java集合框架List,Map,Set等全面介绍(转载)

      Java Collections Framework是Java提供的对集合进行定义,操作,和管理的包含一组接口,类的体系结构.   Java集合框架的基本接口/类层次结构: java.util.C ...

  4. java 集合框架(十六)Map

    一.概述 Map是一个包含键值对的集合,一个map不能有重复的键(key),而且每个键至多只能对应一个值.Map同Collection一样,它的所有通用实现都会提供一个转换器构造函数,接收一个Map类 ...

  5. Java集合框架之map

    Java集合框架之map. Map的主要实现类有HashMap,LinkedHashMap,TreeMap,等等.具体可参阅API文档. 其中HashMap是无序排序. LinkedHashMap是自 ...

  6. 【JAVA集合框架之Map】

    一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

  7. Java集合框架中Map接口的使用

    在我们常用的Java集合框架接口中,除了前面说过的Collection接口以及他的根接口List接口和Set接口的使用,Map接口也是一个经常使用的接口,和Collection接口不同,Map接口并不 ...

  8. 我所理解Java集合框架的部分的使用(Collection和Map)

    所谓集合,就是和数组类似——一组数据.java中提供了一些处理集合数据的类和接口,以供我们使用. 由于数组的长度固定,处理不定数量的数据比较麻烦,于是就有了集合. 以下是java集合框架(短虚线表示接 ...

  9. 【由浅入深理解java集合】(一)——集合框架 Collction、Map

    本篇文章主要对java集合的框架进行介绍,使大家对java集合的整体框架有个了解.具体介绍了Collection接口,Map接口以及Collection接口的三个子接口Set,List,Queue. ...

  10. 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射

    从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Collection 接口又有 3 ...

随机推荐

  1. logstash导入DNS解析数据到es中,中间有filebeat

    这个过程中,主要用logstash处理数据的时候不好处理. 在logstash-sample.conf这个配置文件中的配置,我用这个监控filebeat的5044端口 # Sample Logstas ...

  2. STL——容器(Map & multimap)的删除

    Map & multimap 的删除 map.clear();           //删除所有元素 map.erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的 ...

  3. Jmeter(5)JSON提取器

    Jmeter后置处理器-JSON提取器 JSON是一种轻量级数据格式,以"键-值"对形式组织数据. JSON串中{}表示对象,[]表示对象组成的数组.对象包含多个"属性& ...

  4. gnuplot图例legend设置

    //将图例放在右下角 set key bottom //将图例放在中间 set key center //将图例放在左边 set key left //将图例放在指定位置右下角的坐标为(10,0.7) ...

  5. C#面向对象(初级)

    一.面向对象:创建一个对象,这个对象最终会帮你实现你的需求,尽管其中的过程非常曲折艰难.这也就是所谓的"你办事我放心". 例如: 面向对象:折纸 爸爸开心地用纸折成了一个纸鹤: 妈 ...

  6. 1-解决java Scanner出现 java.util.NoSuchElementException

    起因:在函数中新建scanner对象,然后多次调用此方法出现上述异常 原因:Scanner(system.in)在Scanner中接受的是键盘 输入,当调用close()方法时 Scanner的关闭会 ...

  7. 报错 ncclCommInitRank failed.

    环境 4 GeForce GTX 1080 GPUS docker image nnabla/nnabla-ext-cuda-multi-gpu:py36-cuda102-mpi3.1.6-v1.14 ...

  8. 浅谈IAT加密原理及过程

    上一次做完代码段加密后,又接触到了新的加密方式:IAT加密 IAT加密是通过隐藏程序的导入表信息,以达到增加分析程序的难度.因为没有导入表,就无法单纯的从静态状态下分析调用了什么函数,动态调试时,也无 ...

  9. JVM 完整深入解析

    工作之余,想总结一下JVM相关知识.以下内容都是针对于jdk1.7的. Java运行时数据区: Java虚拟机在执行Java程序的过程中会将其管理的内存划分为若干个不同的数据区域,这些区域有各自的用途 ...

  10. Singleton Pattern -- 不一样的单例模式

    Singleton Pattern -- 单例模式 单例模式是用来创建一个只能又一个实例的对象. 单例模式类图如下. 单例模式有两大好处: (1)对于频繁使用的对象,可以省略创建对象所话费的时间,这对 ...