Java集合2
- Set集合
- HashSet保证元素唯一性的原理
- 练习 存储自定义对象并遍历
- 练习 存储自定义对象保证元素唯一性
- Map集合
- 成员方法
- 获取方法
- Map集合的遍历
- 键找值
- 键值对对象找键和值
- HashMap集合练习
- 键是String值是Student
- 键是Student值是String
- 集合的嵌套练习
- ArrayList嵌套HashMap
- HashMap嵌套ArrayList
Set集合
Set:一个不包含重复元素的collection
HashSet:Set的实现类。不保证set的迭代顺序,特别是不保证该顺序恒久不变
Set<String> set = new HashSet<>();
set.add("hello");
set.add("world");
set.add("java");
//唯一
set.add("world");
for(String s: set) {
System.out.println(s); //输出world java hello 存储和取出的顺序不一致HashSet保证元素唯一性的原理
HashSet添加功能的执行过程中,进行了数据的判断:
首先比较对象的哈希值是否相同,哈希值是根据对象的hashCode()计算出来的。
如果哈希值不同,直接添加到集合中
如果哈希值相同,继续执行equals()进行比较:返回的是true,说明元素重复,不添加;返回的是false,说明元素不重复,添加
如果使用HashSet集合存储对象,要想保证元素的唯一性,数据所属的类必须重写hashCode()和equals()方法
练习 存储自定义对象并遍历
需求:
HashSet集合存储自定义对象并遍历
提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
遍历方式:迭代器,增强forpublic class Student {
private String name;
private int age;
public Student() {}
public Student(String name, int age) {
= name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}public static void main(String[] args) {
HashSet<Student> hs = new HashSet<>();
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",33);
hs.add(s1);
hs.add(s2);
hs.add(s3);
Iterator<Student> it = hs.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s);
}
for(Student s: hs) {
System.out.println(s);
}
}练习 存储自定义对象保证元素唯一性
需求:
HashSet集合存储自定义对象并遍历
要求:如果对象的成员变量值相同,就认为是同一个元素
提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
遍历方式:迭代器,增强for学生类改写equals()和hashCode(),其余与上述练习相同
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, );
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}测试
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<>();
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",33);
Student s4 = new Student("王祖贤",33);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
Iterator<Student> it = hs.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s);
}
for(Student s: hs) {
System.out.println(s);
}
}Map集合
Map:将键映射到值的对象。一个映射不能包含重复的键,每个键最多只能映射到一个值。
举例:学生的学号和姓名
定义格式:public interface Map<K, V>
添加元素:put(K key, V value)Map<String, String> map = new HashMap<>();
map.put("it001","林青霞");
map.put("it002","张曼玉");
map.put("it003","王祖贤");
System.out.println(map); //{it003=王祖贤, it002=张曼玉, it001=林青霞}
//HashMap重写了toString()成员方法
V put(K key, V value):添加元素
Map集合中的实现类的数据结构只针对键有效,保证了键的唯一性
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存储,就用值把以前的值替换,返回以前的值System.out.println("put:"+map.put("张无忌","周芷若")); //put:null
System.out.println("put:"+map.put("张无忌","赵敏")); //put:周芷若
System.out.println(map); //{张无忌=赵敏}map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
System.out.println(map); //{杨过=小龙女, 郭靖=黄蓉, 张无忌=赵敏}V remove(Object key):根据键删除键值对元素System.out.println("remove:"+map.remove("郭靖")); //remove:黄蓉
System.out.println("remove:"+map.remove("郭襄")); //remove:null
System.out.println(map); //{杨过=小龙女, 张无忌=赵敏}void clear():移除所有的键值对元素map.clear();
System.out.println(map); //{}boolean containsKey(Object key):判断集合是否包含指定的键System.out.println("containsKey:"+map.containsKey("郭靖")); //containsKey:true
System.out.println("containsKey:"+map.containsKey("郭襄")); //containsKey:false
System.out.println(map); //{杨过=小龙女, 郭靖=黄蓉, 张无忌=赵敏}boolean containsValue(Object value):判断集合是否包含指定的值System.out.println("containsKey:"+map.containsValue("小龙女")); //containsKey:true
System.out.println("containsKey:"+map.containsValue("小龙男")); //containsKey:false
System.out.println(map); //{杨过=小龙女, 郭靖=黄蓉, 张无忌=赵敏}boolean isEmpty():判断集合是否为空System.out.println("isEmpty:"+map.isEmpty()); //isEmpty:false
map.clear();
System.out.println("isEmpty:"+map.isEmpty()); //isEmpty:trueint size():返回集合中的键值对的对数System.out.println("size:"+map.size()); //size:3获取方法
V get(Object key):根据键获取值System.out.println("get:"+map.get("张无忌")); //get:赵敏
System.out.println("get:"+map.get("张三丰")); //get:nullSet<K> keySet():获取所有键的集合Set<String> set = map.keySet();
for(String key: set) {
System.out.println(key); //杨过 郭靖 张无忌
}Collection<V> values():获取所有值的集合Collection<String> values = map.values();
for(String value: values) {
System.out.println(value); //小龙女 黄蓉 赵敏
}Set<Map.Entry<K, V>> entrySet():获取键值对对象的集合Set<Map.Entry<String, String>> set = map.entrySet();
for(Map.Entry<String, String> me: set) {
System.out.println(me); //杨过=小龙女 郭靖=黄蓉 张无忌=赵敏
}Map集合的遍历
键找值
思路:Map集合看成是一个夫妻对的集合
- 把所有的丈夫给集中起来
- 遍历丈夫的集合,获取到每一个丈夫
- 根据丈夫去找对应的妻子
转换:
- 获取所有键的集合‘
- 遍历键的集合,获取到每一个键
- 根据键去找值
Set<String> set = map.keySet();
for(String key: set) {
String value = map.get(key);
System.out.println(key+"---"+value);
}键值对对象找键和值
思路:
- 获取所有结婚证的集合
- 遍历结婚证的集合,得到每一个结婚证
- 根据结婚证获取丈夫和妻子
转换:
- 获取所有键值对对象的集合
- 遍历键值对对象的集合,得到每一个键值对对象
- 根据键值对对象获取键和值
Set<Map.Entry<String, String>> set = map.entrySet();
for(Map.Entry<String, String> me: set) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"---"+value);
}HashMap集合练习
键是String值是Student
需求:
两种方式遍历
HashMap<String, Student>
键:String 学号
值:Student 学生对象public class Student {
private String name;
private int age;
public Student() {}
public Student(String name, int age) {
= name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, );
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<>();
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",33);
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
Set<String> set = hm.keySet();
for(String key : set) {
Student value = hm.get(key);
System.out.println(key+"---"+value);
}
Set<Map.Entry<String, Student>> set1 = hm.entrySet();
for(Map.Entry<String, Student> me: set1) {
String key = me.getKey();
Student value = me.getValue();
System.out.println(key+"---"+value);
}
}键是Student值是String
需求:
两种方式遍历
HashMap<String, Student>
键:Student 学生对象
值:String 学生住址
要求:如果学生对象的成员变量值相同,就说明是同一个键要想保证元素的唯一性,数据所属的类必须重写hashCode()和equals()方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, );
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",33);
hm.put(s1,"001");
hm.put(s2,"002");
hm.put(s3,"003");
Set<Student> set = hm.keySet();
for(Student key : set) {
String value = hm.get(key);
System.out.println(key+"---"+value);
}
Set<Map.Entry<Student, String>> set1 = hm.entrySet();
for(Map.Entry<Student, String> me: set1) {
Student key = me.getKey();
String value = me.getValue();
System.out.println(key+"---"+value);
}
}集合的嵌套练习
ArrayList嵌套HashMap
需求:ArrayList集合嵌套HashMap集合并遍历
定义一个ArrayList集合,包含三个元素,每一个元素都是HashMap类型的
每一个HashMap集合的键和值都是String类型的
键:String 丈夫的姓名
值:String 妻子的姓名
输出如下的字符串数据:
第一个HashMap集合的元素:
孙策 大乔
周瑜 小乔
第二个HashMap集合的元素:
郭靖 黄蓉
杨过 小龙女
第三个HashMap集合的元素:
令狐冲 任盈盈
林平之 岳灵珊public static void main(String[] args) {
ArrayList<HashMap<String, String>> array = new ArrayList<>();
HashMap<String, String> hm1 = new HashMap<>();
hm1.put("孙策","大乔");
hm1.put("周瑜","小乔");
HashMap<String, String> hm2 = new HashMap<>();
hm2.put("郭靖","黄蓉");
hm2.put("杨过","小龙女");
HashMap<String, String> hm3 = new HashMap<>();
hm3.put("令狐冲","任盈盈");
hm3.put("林平之","岳灵珊");
array.add(hm1);
array.add(hm2);
array.add(hm3);
for(HashMap<String, String> hm: array) {
Set<String> set = hm.keySet();
for(String key: set) {
String value = hm.get(key);
System.out.println(key+"---"+value);
}
System.out.println("------");
}
}HashMap嵌套ArrayList
需求:HashMap集合嵌套ArrayList集合并遍历
定义一个HashMap集合,包含三个元素,每一个元素都是ArrayList类型的
键:String 人物来自哪部电视剧
值:ArrayList 人物的名称
每一个ArrayList集合的数据是String类型的
输出如下的字符串数据:
第一个ArrayList集合的元素:(三国演义)
诸葛亮
赵云
第二个ArrayList集合的元素:(西游记)
唐僧
孙悟空
第三个ArrayList集合的元素:(水浒传)
武松
鲁智深public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<>();
ArrayList<String> array1 = new ArrayList<>();
ArrayList<String> array2 = new ArrayList<>();
ArrayList<String> array3 = new ArrayList<>();
array1.add("诸葛亮");
array1.add("赵云");
array2.add("唐僧");
array2.add("孙悟空");
array3.add("武松");
array3.add("鲁智深");
hm.put("三国演义",array1);
hm.put("西游记",array2);
hm.put("水浒传",array3);
Set<String> set = hm.keySet();
for(String key : set) {
System.out.println(key);
ArrayList<String> value = hm.get(key);
for(String s: value) {
System.out.println("\t"+s);
}
}
}
















