HashSet
哈希值以及对象的哈希值特点
链表和红黑树里面使用equals方法比较属性值,然后决定是否添加。如果HashSet集合要存入自定义对象,那么他就必须重写hashcode和equals方法
SET总结
红黑树默认使用自然排序,当自然排序不适用了,就用指定的比较器排序
Map集合概述和使用
Map集合双列集合一次能添加两个元素,也就是一对儿数据。而当列集合一次只能存入一个数据。
Map常用方法
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("itheima001","小智");
map.put("itheima002","小美");
map.put("itheima003","大胖");
map.put("itheima004","小黑");
map.put("itheima005","大帅");
// put方法如果key不存在则储存键值对,如果已经存在则把value弹出,并存入新的键值对
String put = map.put("itheima001", "aaa");
System.out.println(put);
System.out.println(map);
// 根据键值删除数据,并返回value
String s = map.remove("itheima001");
System.out.println(s);
System.out.println(map);
// map.clear();
// System.out.println(map);
boolean result1 = map.containsKey("itheima002");
boolean result2 = map.containsKey("itheima006");
System.out.println(result1);
System.out.println(result2);
boolean result3 = map.containsValue("大帅");
boolean result4 = map.containsValue("大胖");
System.out.println(result3);
System.out.println(result4);
// boolean empty = map.isEmpty();
// System.out.println(empty);
// map.clear();
// boolean empty2 = map.isEmpty();
// System.out.println(empty2);
int length = map.size();
System.out.println(length);
}
Map的遍历(两种方式)
blic static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1号丈夫","1号妻子");
map.put("2号丈夫","2号妻子");
map.put("3号丈夫","3号妻子");
map.put("4号丈夫","4号妻子");
map.put("5号丈夫","5号妻子");
Set<String> keys = map.keySet();
// 第一种遍历方式
for (String key : keys) {
// 通过每一个键来获取对应的值
String value = map.get(key);
System.out.println(key+"........"+value);
}
}
Set<Map.Entry<String,String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"xxxxx"+value);
}
HashMap小结
三种遍历方式以及自定义类作为键
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<>();
Student s1 = new Student("小李",22);
Student s2 = new Student("小红",23);
Student s3 = new Student("小蓝",22);
hm.put(s1,"山东");
hm.put(s2,"北京");
hm.put(s3,"天津");
Set<Student> keys = hm.keySet();
for (Student key : keys) {
String value = hm.get(key);
System.out.println(key+"籍贯:"+value);
}
Set<Map.Entry<Student,String>> entires = hm.entrySet();
for (Map.Entry<Student, String> entry : entires) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"籍贯:"+value);
}
hm.forEach((Student s,String str)->{
System.out.println(s+"籍贯:"+str);
});