第1章 Set集合特点和HashSet集合类
1.1 Set集合的特点
- Set:元素唯一,存储元素无序
一个不包含重复元素的 collection - HashSet:
它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变
public class SetDemo {
public static void main(String[] args) {
//创建集合对象
Set<String> set = new HashSet<String>();
//添加元素
set.add("hello");
set.add("world");
set.add("java");
//唯一
set.add("world");
//遍历集合
for(String s : set) {
System.out.println(s);
}
}
}
1.2 HashSet保证元素唯一性的原理
- HashSet保证元素唯一性的原理?
通过查看add方法的源码,我们知道了添加功能的执行过程中,是进行了数据的判断的。 - 这个判断的流程是:
首先比较对象的哈希值是否相同,这个哈希值是根据对象的hashCode()计算出来的。
如果哈希值不同,就直接添加到集合中
如果哈希值相同,继续执行equals()进行比较,
返回的是true,说明元素重复,不添加。
返回的是false,说明元素不重复,就添加。
如果我们使用HashSet集合存储对象,你要想保证元素的唯一性,就必须重写hashCode()和equals()方法。
第2章 Map集合
2.1 创建Map集合对象并添加元素
-
Map:将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
-
举例:学生的学号和姓名
it001 林青霞
it002 张曼玉
it003 王祖贤
public interface Map<K,V>
public class MapDemo {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//添加元素
//put(K key,V value):添加元素。
map.put("it001", "林青霞");
map.put("it002", "张曼玉");
map.put("it003", "王祖贤");
//输出集合对象
System.out.println(map);
}
}
2.2 Map集合的成员方法
- V put(K key,V value):添加元素
- V remove(Object key):根据键删除键值对元素
- void clear():移除所有的键值对元素
- boolean containsKey(Object key):判断集合是否包含指定的键
- boolean containsValue(Object value):判断集合是否包含指定的值
- boolean isEmpty():判断集合是否为空
- int size():返回集合中的键值对的对数
2.3 Map集合的获取功能测试
- V get(Object key):根据键获取值
- Set keySet():获取所有键的集合
- Collection values():获取所有值的集合
public class MapDemo3 {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//添加元素
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");
map.put("张无忌", "赵敏");
//V get(Object key):根据键获取值
System.out.println("get:"+map.get("张无忌"));
System.out.println("get:"+map.get("张三丰"));
System.out.println("--------------------");
//Set<K> keySet():获取所有键的集合
Set<String> set = map.keySet();
for(String key : set) {
System.out.println(key);
}
System.out.println("--------------------");
//Collection<V> values():获取所有值的集合
Collection<String> values = map.values();
for(String value : values) {
System.out.println(value);
}
2.4 Map集合的遍历之键找值
Map集合的遍历:
- 思路:Map看成是一个夫妻对的集合
A:把所有的丈夫给集中起来
B:遍历丈夫的集合,获取到每一个丈夫
C:根据丈夫去找对应的妻子 - 转换:
A:获取所有键的集合
B:遍历键的集合,获取到每一个键
C:根据键去找值
public class MapDemo {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//添加元素
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
//获取所有键的集合
Set<String> set = map.keySet();
//遍历键的集合,获取到每一个键
for(String key : set) {
//根据键去找值
String value = map.get(key);
System.out.println(key+"---"+value);
}
}
}
2.5 Map集合的遍历之键值对对象找键和值
Map集合遍历的方式2:
- 思路:
A:获取所有结婚证的集合
B:遍历结婚证的集合,得到每一个结婚证
C:根据结婚证获取丈夫和妻子 - 转换:
A:获取所有键值对对象的集合
B:遍历键值对对象的集合,得到每一个键值对对象
C:根据键值对对象获取键和值
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//添加元素
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
//获取所有键值对对象的集合
// Set<Map.Entry<K,V>> entrySet()
// 获取键值对对象的集合
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);
}
}
}
2.6 Map集合的两种遍历方式图解
第3章 HashMap集合的相关案例
3.1 HashMap集合的练习
练习1:两种方式遍历
HashMap<String,Student>
键:String 学号
值:Student 学生对象
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public class HashMapTest {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, Student> hm = new HashMap<String, Student>();
// 创建元素对象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("张曼玉", 35);
Student s3 = new Student("王祖贤", 33);
// 添加元素到集合中
hm.put("it001", s1);
hm.put("it002", s2);
hm.put("it003", s3);
// 遍历
// 根据键找值
Set<String> set = hm.keySet();
for (String key : set) {
Student value = hm.get(key);
System.out.println(key + "---" + value.getName() + "---" + value.getAge());
}
System.out.println("---------------------");
// 根据键值对对象找键和值
Set<Map.Entry<String, Student>> set2 = hm.entrySet();
for (Map.Entry<String, Student> me : set2) {
String key = me.getKey();
Student value = me.getValue();
System.out.println(key + "---" + value.getName() + "---" + value.getAge());
}
}
}