1.知识点:使用LinkedHashSet的方法
LinkedHashSet<String> set = new LinkedHashSet<>();set.add("椎名空");set.add("李林俊");set.add("天使萌");set.add("佐佐木希");set.add("古川伊织");set.add("京香Julia");LinkedHashSet<String> newSet1 = new LinkedHashSet<>();Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");set.addAll(newSet1);int size = set.size();System.out.println("获取元素个数:" + size);System.out.println("判断集合中是否有指定元素:" + set.contains("天使萌"));System.out.println("判断集合中是否有指定集合(判断包含关系):" + set.containsAll(newSet1));System.out.println("判断集合中是否没有元素:" + set.isEmpty());set.remove("上原亚衣");set.removeAll(newSet1);LinkedHashSet<String> newSet2 = new LinkedHashSet<>();Collections.addAll(newSet2, "佐佐木希","古川伊织","天使萌","天使萌");set.retainAll(newSet2);Object[] array = newSet2.toArray();System.out.println(Arrays.toString(array));System.out.println("---------------");for (String element : set) {System.out.println(element);}System.out.println("---------------");Iterator<String> it = set.iterator();while(it.hasNext()){String next = it.next();System.out.println(next);}}* 知识点:感受LinkedHashSet的特点* * 特点:有序 + 去重* * 存储数据:* 1.获取对象的hash值* 2.通过hash值+散列算法获取到数组中的下标* 3.将元素添加到节点对象中(节点对象-双向链表)* 取出数据:* 获取到第一个节点对象,再依次循环到下一个节点(有序的)*/LinkedHashSet<String> set = new LinkedHashSet<>();set.add("椎名空");set.add("李林俊");set.add("天使萌");set.add("佐佐木希");set.add("古川伊织");set.add("京香Julia");set.add("京香Julia");for (String str : set) {System.out.println(str);}
2.知识点:使用TreeSet的方法
TreeSet<String> set = new TreeSet<>();set.add("椎名空");set.add("李林俊");set.add("天使萌");set.add("佐佐木希");set.add("古川伊织");set.add("京香Julia");TreeSet<String> newSet1 = new TreeSet<>();Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");set.addAll(newSet1);int size = set.size();System.out.println("获取元素个数:" + size);System.out.println("判断集合中是否有指定元素:" + set.contains("天使萌"));System.out.println("判断集合中是否有指定集合(判断包含关系):" + set.containsAll(newSet1));System.out.println("判断集合中是否没有元素:" + set.isEmpty());set.remove("上原亚衣");set.removeAll(newSet1);TreeSet<String> newSet2 = new TreeSet<>();Collections.addAll(newSet2, "佐佐木希","古川伊织","天使萌","天使萌");set.retainAll(newSet2);Object[] array = newSet2.toArray();System.out.println(Arrays.toString(array));System.out.println("---------------");for (String element : set) {System.out.println(element);}System.out.println("---------------");Iterator<String> it = set.iterator();while(it.hasNext()){String next = it.next();System.out.println(next);}
TreeSet<String> set1 = new TreeSet<>();set1.add("b");set1.add("d");set1.add("a");set1.add("c");set1.add("c");for (String element : set1) {System.out.println(element);}TreeSet<Integer> set2 = new TreeSet<>();set2.add(5);set2.add(1);set2.add(3);set2.add(2);set2.add(4);set2.add(4);for (Integer element : set2) {System.out.println(element);}
3.知识点:TreeSet内置比较器 - Comparable
TreeSet<Student> set = new TreeSet<>();set.add(new Student("天海翼", '女', 28, "2204", "001"));set.add(new Student("天使萌", '女', 21, "2204", "002"));set.add(new Student("水菜丽", '女', 25, "2204", "003"));set.add(new Student("佐佐木希", '女', 22, "2204", "004"));set.add(new Student("上原亚衣", '女', 27, "2204", "005"));set.add(new Student("濑亚美莉", '女', 18, "2204", "006"));set.add(new Student("深田咏美", '女', 23, "2204", "007"));set.add(new Student("泷泽萝拉", '女', 25, "2204", "008"));set.add(new Student("冲田杏梨", '女', 27, "2204", "009"));for (Student stu : set) {System.out.println(stu);}
----------------------------------------------------------------public class Student implements Comparable<Student>{private String name;private char sex;private int age;private String classId;private String id;@Overridepublic String toString() {return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", classId=" + classId + ", id=" + id + "]";}@Overridepublic int compareTo(Student o) {return this.age - o.age;}
4.知识点:TreeSet外置比较器 - Comparator
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {if(o1.equals(o2)){return 0;}int nameLen1 = o1.getName().length();int nameLen2 = o2.getName().length();if(nameLen1 != nameLen2){return nameLen1 - nameLen2;}int age1 = o1.getAge();int age2 = o2.getAge();if(age1 != age2){return age1 - age2;}return 1;}});set.add(new Student("水菜丽", '女', 25, "2204", "003"));set.add(new Student("佐佐木希", '女', 22, "2204", "004"));set.add(new Student("天使萌", '女', 21, "2204", "002"));set.add(new Student("上原亚衣", '女', 27, "2204", "005"));set.add(new Student("濑亚美莉", '女', 18, "2204", "006"));set.add(new Student("天海翼", '女', 28, "2204", "001"));set.add(new Student("深田咏美", '女', 23, "2204", "007"));set.add(new Student("泷泽萝拉", '女', 25, "2204", "008"));set.add(new Student("冲田杏梨", '女', 27, "2204", "009"));set.add(new Student("冲田杏梨", '女', 27, "2204", "009"));for (Student stu : set) {System.out.println(stu);}}
5.知识点:HashMap的使用
HashMap<String, Integer> map = new HashMap<>();Integer put1 = map.put("麻生希", 28);Integer put2 = map.put("椎名空", 23);Integer put3 = map.put("天使萌", 26);Integer put4 = map.put("水菜丽", 26);Integer put5 = map.put("爱田奈奈", 25);Integer put6 = map.put("小峰由衣", 29);System.out.println(put1);System.out.println(put2);System.out.println(put3);System.out.println(put4);System.out.println(put5);System.out.println(put6);HashMap<String, Integer> newMap = new HashMap<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ccc", 40);map.putAll(newMap);Integer putIfAbsent = map.putIfAbsent("椎名空1", 25);System.out.println("putIfAbsent -- " + putIfAbsent);Integer put7 = map.put("小峰由衣", 30);System.out.println(put7);Integer replace1 = map.replace("小峰由衣", 31);System.out.println(replace1);boolean replace2 = map.replace("小峰由衣", 31, 32);System.out.println(replace2);Integer integer1 = map.get("天使萌");System.out.println("通过key获取到value:" + integer1);Integer integer2 = map.getOrDefault("天使萌111", 888);System.out.println("通过key获取到value,如果key不存在则返回默认值:" + integer2);System.out.println("判断集合中是否包含某个key:" + map.containsKey("天使萌"));System.out.println("判断集合中是否包含某个value:" + map.containsValue(26));System.out.println("判断集合是否没有元素:" + map.isEmpty());map.remove("椎名空1");map.remove("小峰由衣", 32);System.out.println("获取元素(映射关系)个数:" + map.size());Collection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));System.out.println("-----------------");Set<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------");Set<Entry<String, Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
6.知识点:LinkedHashMap的使用
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();Integer put1 = map.put("麻生希", 28);Integer put2 = map.put("椎名空", 23);Integer put3 = map.put("天使萌", 26);Integer put4 = map.put("水菜丽", 26);Integer put5 = map.put("爱田奈奈", 25);Integer put6 = map.put("小峰由衣", 29);System.out.println(put1);System.out.println(put2);System.out.println(put3);System.out.println(put4);System.out.println(put5);System.out.println(put6);LinkedHashMap<String, Integer> newMap = new LinkedHashMap<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ccc", 40);map.putAll(newMap);Integer putIfAbsent = map.putIfAbsent("椎名空1", 25);System.out.println("putIfAbsent -- " + putIfAbsent);Integer put7 = map.put("小峰由衣", 30);System.out.println(put7);Integer replace1 = map.replace("小峰由衣", 31);System.out.println(replace1);boolean replace2 = map.replace("小峰由衣", 31, 32);System.out.println(replace2);Integer integer1 = map.get("天使萌");System.out.println("通过key获取到value:" + integer1);Integer integer2 = map.getOrDefault("天使萌111", 888);System.out.println("通过key获取到value,如果key不存在则返回默认值:" + integer2);System.out.println("判断集合中是否包含某个key:" + map.containsKey("天使萌"));System.out.println("判断集合中是否包含某个value:" + map.containsValue(26));System.out.println("判断集合是否没有元素:" + map.isEmpty());map.remove("椎名空1");map.remove("小峰由衣", 32);System.out.println("获取元素(映射关系)个数:" + map.size());Collection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));System.out.println("-----------------");Set<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------");Set<Entry<String, Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
7.知识点:Hashtable的使用
/*** 知识点:Hashtable的使用* * 特点:无序 + key唯一(去重) + 线程安全的(加锁)* * 注意:Hashtable方法上加锁,效率低,弃用*///创建Hashtable对象Hashtable<String, Integer> map = new Hashtable<>();//添加数据Integer put1 = map.put("麻生希", 28);Integer put2 = map.put("椎名空", 23);Integer put3 = map.put("天使萌", 26);Integer put4 = map.put("水菜丽", 26);Integer put5 = map.put("爱田奈奈", 25);Integer put6 = map.put("小峰由衣", 29);System.out.println(put1);//nullSystem.out.println(put2);//nullSystem.out.println(put3);//nullSystem.out.println(put4);//nullSystem.out.println(put5);//nullSystem.out.println(put6);//null//将newMap集合添加到map集合中Hashtable<String, Integer> newMap = new Hashtable<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ccc", 40);map.putAll(newMap);//如果集合中有该key就获取对应的value值//如果集合中没有该key就添加数据Integer putIfAbsent = map.putIfAbsent("椎名空1", 25);System.out.println("putIfAbsent -- " + putIfAbsent);//替换数据 -- 返回被替换的值Integer put7 = map.put("小峰由衣", 30);System.out.println(put7);//29//根据Key替换数据 -- 返回被替换的值Integer replace1 = map.replace("小峰由衣", 31);System.out.println(replace1);//30//根据Key+Value替换数据boolean replace2 = map.replace("小峰由衣", 31, 32);System.out.println(replace2);//true//通过key获取到valueInteger integer1 = map.get("天使萌");System.out.println("通过key获取到value:" + integer1);//通过key获取到value,如果key不存在则返回默认值Integer integer2 = map.getOrDefault("天使萌111", 888);System.out.println("通过key获取到value,如果key不存在则返回默认值:" + integer2);//清空集合中的数据//map.clear();System.out.println("判断集合中是否包含某个key:" + map.containsKey("天使萌"));System.out.println("判断集合中是否包含某个value:" + map.containsValue(26));System.out.println("判断集合是否没有元素:" + map.isEmpty());//删除元素map.remove("椎名空1");//通过key删除映射关系map.remove("小峰由衣", 32);//通过key+value删除映射关系System.out.println("获取元素(映射关系)个数:" + map.size());//获取map中所有的value,返回一个Collection集合Collection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));System.out.println("-----------------");//遍历1 -- keySet()//遍历思路:// 1.将map中所有的key获取出并存放在Set集合中// 2.遍历Set集合,依次取出key,再使用map.get(key)获取出对应的value值Set<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------");//遍历2 -- entrySet()//遍历思路:// 1.将map中所有的映射关系对象获取出并存放在Set集合中// 2.遍历Set集合,依次取出映射关系对象,映射关系对象中包含的key和value也就取出了Set<Entry<String, Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
8.知识点:ConcurrentHashMap的使用
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();Integer put1 = map.put("麻生希", 28);Integer put2 = map.put("椎名空", 23);Integer put3 = map.put("天使萌", 26);Integer put4 = map.put("水菜丽", 26);Integer put5 = map.put("爱田奈奈", 25);Integer put6 = map.put("小峰由衣", 29);System.out.println(put1);System.out.println(put2);System.out.println(put3);System.out.println(put4);System.out.println(put5);System.out.println(put6);ConcurrentHashMap<String, Integer> newMap = new ConcurrentHashMap<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ccc", 40);map.putAll(newMap);Integer putIfAbsent = map.putIfAbsent("椎名空1", 25);System.out.println("putIfAbsent -- " + putIfAbsent);Integer put7 = map.put("小峰由衣", 30);System.out.println(put7);Integer replace1 = map.replace("小峰由衣", 31);System.out.println(replace1);boolean replace2 = map.replace("小峰由衣", 31, 32);System.out.println(replace2);Integer integer1 = map.get("天使萌");System.out.println("通过key获取到value:" + integer1);Integer integer2 = map.getOrDefault("天使萌111", 888);System.out.println("通过key获取到value,如果key不存在则返回默认值:" + integer2);System.out.println("判断集合中是否包含某个key:" + map.containsKey("天使萌"));System.out.println("判断集合中是否包含某个value:" + map.containsValue(26));System.out.println("判断集合是否没有元素:" + map.isEmpty());map.remove("椎名空1");map.remove("小峰由衣", 32);System.out.println("获取元素(映射关系)个数:" + map.size());Collection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));System.out.println("-----------------");Set<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------");Set<Entry<String, Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
9.HashMap vs LinkedHashMap vs Hashtable vs ConcurrentHashMap
ConcurrentHashMap<Object,Object> map = new ConcurrentHashMap<>();map.put(null, null);
10.知识点:TreeMap的使用
TreeMap<String, Integer> map = new TreeMap<>();Integer put1 = map.put("麻生希", 28);Integer put2 = map.put("椎名空", 23);Integer put3 = map.put("天使萌", 26);Integer put4 = map.put("水菜丽", 26);Integer put5 = map.put("爱田奈奈", 25);Integer put6 = map.put("小峰由衣", 29);System.out.println(put1);System.out.println(put2);System.out.println(put3);System.out.println(put4);System.out.println(put5);System.out.println(put6);TreeMap<String, Integer> newMap = new TreeMap<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ccc", 40);map.putAll(newMap);Integer putIfAbsent = map.putIfAbsent("椎名空1", 25);System.out.println("putIfAbsent -- " + putIfAbsent);Integer put7 = map.put("小峰由衣", 30);System.out.println(put7);Integer replace1 = map.replace("小峰由衣", 31);System.out.println(replace1);boolean replace2 = map.replace("小峰由衣", 31, 32);System.out.println(replace2);Integer integer1 = map.get("天使萌");System.out.println("通过key获取到value:" + integer1);Integer integer2 = map.getOrDefault("天使萌111", 888);System.out.println("通过key获取到value,如果key不存在则返回默认值:" + integer2);System.out.println("判断集合中是否包含某个key:" + map.containsKey("天使萌"));System.out.println("判断集合中是否包含某个value:" + map.containsValue(26));System.out.println("判断集合是否没有元素:" + map.isEmpty());map.remove("椎名空1");map.remove("小峰由衣", 32);System.out.println("获取元素(映射关系)个数:" + map.size());Collection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));System.out.println("-----------------");Set<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------");Set<Entry<String, Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
11.知识点:TreeMap的特点
TreeMap<String, Integer> map = new TreeMap<>();map.put("c", 24);map.put("d", 28);map.put("a", 21);map.put("b", 23);map.put("b", 26);Set<Entry<String,Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}
12.知识点:TerrMap内置比较器
TreeMap<Student, String> map = new TreeMap<>();map.put(new Student("水菜丽", '女', 25, "2204", "003"), "拍电影");map.put(new Student("佐佐木希", '女', 22, "2204", "004"), "吃马赛克");map.put(new Student("天使萌", '女', 21, "2204", "002"), "吹喇叭");map.put(new Student("上原亚衣", '女', 27, "2204", "005"), "骑马");map.put(new Student("濑亚美莉", '女', 18, "2204", "006"), "交朋友");map.put(new Student("天海翼", '女', 28, "2204", "001"), "按摩");map.put(new Student("深田咏美", '女', 23, "2204", "007"), "写代码");map.put(new Student("泷泽萝拉", '女', 25, "2204", "008"), "看书");map.put(new Student("冲田杏梨", '女', 27, "2204", "009"), "补课");Set<Entry<Student,String>> entrySet = map.entrySet();for (Entry<Student, String> entry : entrySet) {System.out.println(entry);}
-------------------------------------------------------------public class Student implements Comparable<Student>{private String name;private char sex;private int age;private String classId;private String id; @Overridepublic String toString() {return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", classId=" + classId + ", id=" + id + "]";}@Overridepublic int compareTo(Student o) {return this.age - o.age;}
13.知识点:TreeMap外置比较器
TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {if(o1.equals(o2)){return 0;}int nameLen1 = o1.getName().length();int nameLen2 = o2.getName().length();if(nameLen1 != nameLen2){return nameLen1 - nameLen2;}int age1 = o1.getAge();int age2 = o2.getAge();if(age1 != age2){return age1 - age2;}return 1;}});map.put(new Student("水菜丽", '女', 25, "2204", "003"), "拍电影");map.put(new Student("佐佐木希", '女', 22, "2204", "004"), "吃马赛克");map.put(new Student("天使萌", '女', 21, "2204", "002"), "吹喇叭");map.put(new Student("上原亚衣", '女', 27, "2204", "005"), "骑马");map.put(new Student("濑亚美莉", '女', 18, "2204", "006"), "交朋友");map.put(new Student("天海翼", '女', 28, "2204", "001"), "按摩");map.put(new Student("深田咏美", '女', 23, "2204", "007"), "写代码");map.put(new Student("泷泽萝拉", '女', 25, "2204", "008"), "看书");map.put(new Student("冲田杏梨", '女', 27, "2204", "009"), "补课");Set<Entry<Student,String>> entrySet = map.entrySet();for (Entry<Student, String> entry : entrySet) {System.out.println(entry);}
14.知识点:Collections - 集合工具类
ArrayList<Integer> list = new ArrayList<>();Collections.addAll(list, 1,5,3,2,6,9,7,8);Collections.sort(list);Collections.sort(list, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1, o2);}});Integer max = Collections.max(list);Integer min = Collections.min(list);System.out.println("最大值:" + max);System.out.println("最小值:" + min);for (Integer integer : list) {System.out.println(integer);}
15.知识点:Properties-配置文件类
Properties p = new Properties();p.load(Test01.class.getClassLoader().getResourceAsStream("DBConfig.properties"));String username = p.getProperty("username");String password = p.getProperty("password");System.out.println(username + " -- " + password);}