百知教育 - 孙帅 - 14_集合框架(续)
13_List的排序
-
代码:
package day16; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Comparator; public class TestSort1{ public static void main(String[] args){ /* List<String> list = Arrays.asList("abc","xya","xsji","sji","jjo"); Collections.sort(list); for(String s : list){ System.out.println(s); } */ List<Student> list = Arrays.asList( new Student("YKY",20), new Student("LYH",20), new Student("HHH",19), new Student("YXM",18) ); Comparator<Student> c1 = new Comparator<Student>(){ //if o1<o2 return 负数 //if o1==o2 return 0 //if o1>o2 reutrn 正数 public int compare(Student o1, Student o2){ return o1.age - o2.age; } }; Comparator<Student> c2 = new Comparator<Student>(){ public int compare(Student o1, Student o2){ return o1.name.compareTo(o2.name); } }; if(true) Collections.sort(list, c1); else Collections.sort(list, c2); for(Student s : list){ System.out.println(s); } } } class Student implements Comparable<Student>{ String name; int age; public Student(String name, int age){ this.name = name; this.age = age; } @Override public String toString(){ return "String [name="+name+", age="+age+"]"; } @Override public int compareTo(Student o){ return this.name.compareTo(o.name); } }
-
运行结果:
14_JDK8中对List的排序
-
排序
- Collections.sort(List):要求集合中的元素实现Comparable接口
- Collections.sort(List, Comparator):Comparator 比较器,实现排序逻辑;集合中的元素不需要实现Comparable接口
- list.sort(Comparator):直接对list调用sort方法排序,只能传入Comparator;是JDK8中的方法
-
代码:
package day16; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Comparator; public class TestStringSort{ public static void main(String[] args){ List<String> list = Arrays.asList("一","四","六","七","三"); Comparator<String> c1 = Comparator.naturalOrder(); //自然规则<==>Collections.sort(list); Comparator<String> c2 = c1.reversed(); Comparator<String> c3 = new Comparator<String>(){ List<String> list = Arrays.asList("一","二","三","四","五","六","七","八","九"); public int compare(String s1, String s2){ return list.indexOf(s1)-list.indexOf(s2); } }; Comparator<String> c4 = c3.reversed(); list.sort(c4); for(String s : list){ System.out.println(s); } } }
-
运行结果:
15_Set介绍
-
Set接口的特点:
用于存放任意Object对象,无序、无下标、元素不能重复 -
Set接口中的方法:
只有 从Collection 父接口中 继承到 的所有方法 -
实现类:
HashSet【重点】 -
Set遍历:
- 迭代器遍历 Collection 可以通过迭代器,删除集合中的元素
- for-each Collection JDK5
- foreach() 方法 需要实现 java.util.function.Consumer接口 来自JDK8
-
代码:
package day17; import java.util.HashSet; import java.util.Set; import java.util.Iterator; import java.util.Collection; import java.util.function.Consumer; public class TestHashSet{ public static void main(String[] args){ Set<String> s = new HashSet<>(); s.add(new String("YKY")); s.add(new String("LYH")); s.add(new String("HHH")); s.add(new String("YXM")); s.add(new String("YKY")); //System.out.println(s.size()); //print print1(s); System.out.println("-------------"); print2(s); System.out.println("-------------"); print3(s); } /* static void print(Collection<String> c){ Iterator<String> it = c.iterator(); while(it.hasNext()){ String str = it.next(); System.out.println(str); } } */ static <T> void print1(Collection<T> c){ Iterator<T> it = c.iterator(); while(it.hasNext()){ T t = it.next(); System.out.println(t); } } static <T> void print2(Collection<T> c){ for(T t : c){ System.out.println(t); } } static <T> void print3(Collection<T> c){ c.forEach(new Consumer<T>(){ public void accept(T t){ System.out.println(t); } }); } }
-
运行结果:
16_HashSet中的哈希算法
-
HashSet(保证内容重复对象只有一个)
- 覆盖 hashCode() 方法,保证 相同对象返回相同的int,尽可能 保证 不同对象返回不同的int
- 覆盖 equals() 方法,保证 相同对象返回true
-
代码:
package day17; import java.util.HashSet; import java.util.Set; import java.util.Iterator; import java.util.Collection; import java.util.function.Consumer; public class TestHashSet{ public static void main(String[] args){ Set<Student> s = new HashSet<>(); s.add(new Student("YKY", 15)); s.add(new Student("LYH", 19)); s.add(new Student("HHH", 19)); s.add(new Student("YXM", 18)); s.add(new Student("YKY", 15)); print1(s); //System.out.println("-------------"); //print2(s); //System.out.println("-------------"); //print3(s); } /* static void print(Collection<String> c){ Iterator<String> it = c.iterator(); while(it.hasNext()){ String str = it.next(); System.out.println(str); } } */ static <T> void print1(Collection<T> c){ Iterator<T> it = c.iterator(); while(it.hasNext()){ T t = it.next(); System.out.println(t); } } static <T> void print2(Collection<T> c){ for(T t : c){ System.out.println(t); } } static <T> void print3(Collection<T> c){ c.forEach(new Consumer<T>(){ public void accept(T t){ System.out.println(t); } }); } } class Student{ String name; int age; public Student(String name, int age){ this.name = name; 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) return false; if(this.getClass() != o.getClass()) return false; Student s = (Student)o; if(this.name.equals(s.name) && this.age == s.age) return true; else return false; } @Override public int hashCode(){ return age; } }
-
运行结果:
17_HashSet中的高级哈希算法
-
eclipse中可自动生成较优的HashCode方法
-
代码(见上代码):
@Override public int hashCode(){ //return age+name.hashCode(); //eclipse自动生成的hashCode实现代码 final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; }
18_Set的其他实现类
-
LinkedHashSet
- 是HashSet的子类
- 维护元素添加到Set中的顺序
-
代码:
package day17; import java.util.LinkedHashSet; import java.util.Set; public class TestLinkedHashSet{ public static void main(String[] args){ Set<String> s = new LinkedHashSet<>(); s.add("YKY"); s.add("LYH"); s.add("YXM"); s.add("HHH"); s.add("WKY"); s.add("YKY"); for(String s1 : s){ System.out.println(s1); } } }
-
运行结果:
-
TreeSet
- 是Set的子接口SortedSet的实现类
- 自动实现对元素的排序
- 依照排序规则判断重复对象
-
代码:
package day17; import java.util.Set; import java.util.TreeSet; import java.util.Comparator; public class TestTreeSet{ public static void main(String[] args){ Comparator<Worker> c = new Comparator<Worker>(){ /* if(w1<w2) return 负数 if(w1>w2) return 正数 if(w1==w2) return 0 */ public int compare(Worker w1, Worker w2){ if(w1.age != w2.age) return w1.age - w2.age; else return w1.name.compareTo(w2.name); } }; Set<Worker> s = new TreeSet<>(c); s.add(new Worker("YKY", 20)); s.add(new Worker("YXM", 19)); s.add(new Worker("ZYK", 19)); s.add(new Worker("HHH", 17)); s.add(new Worker("LYH", 18)); s.add(new Worker("YKY", 20)); for(Worker w : s){ System.out.println(w); } } } class Worker{ String name; int age; public Worker(String name, int age){ this.age = age; this.name = name; } @Override public String toString(){ return "Worker [name="+name+",age="+age+"]"; } }
-
运行结果:
19_Queue接口
-
Queue:
- 队列
- FIFO
-
常用方法:
- add(): 添加元素
- offer(): 添加元素 优先使用
- remove(): 删除元素
- poll(): 删除元素 优先使用
- element(): 获取队列的头元素
- peek(): 获取队列的头元素 优先使用
-
实现类:
LinkedList -
代码:
package day17; import java.util.LinkedList; import java.util.Queue; public class TestQueue{ public static void main(String[] args){ Queue<String> q = new LinkedList<>(); q.offer("YKY"); q.offer("LYH"); q.offer("HHH"); //System.out.println(q.peek()); String s1 = q.poll(); String s2 = q.poll(); String s3 = q.poll(); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(q.size()); } }
-
运行结果:
20_Map的基本特点和遍历
-
基本特点:
- 元素是 key-value
- key无顺序,不可重复
- value无顺序,可重复
-
常用方法:
- get(Object key): 通过key查找对应的value
- put(Object key, Object value): 将key-value添加到Map中,如果key已存在,新的value覆盖旧的value
- remove(Object key): 删除key所对应的key-value对
- size(): 长度
- containsKey(Object key): 判断key是否存在
- containsValue(Object value): 判断value是否存在
-
遍历:
- keySet(): 返回Set,遍历Map中所有的key
- values(): 返回Collection,遍历Map中所有的value
- entrySet(): 返回Set,Set中的元素为Map.Entry对象,代表了一个键值对
-
代码:
package day17; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Collection; public class TestMap{ public static void main(String[] args){ Map<Integer, String> map = new HashMap<>(); map.put(2008 , "北京"); map.put(2012 , "伦敦"); map.put(2016 , "里约热内卢"); map.put(2020 , "东京"); map.put(2024 , "巴黎"); map.put(2028 , "洛杉矶"); map.put(2004 , "雅典"); map.put(2000 , "悉尼"); map.put(1996 , "亚特兰大"); map.put(1992 , "巴塞罗那"); map.put(1988 , "首尔"); map.put(1984 , "洛杉矶"); map.put(1980 , "莫斯科"); /* String s = map.get(2008); System.out.println(s); System.out.println(map.containsKey(1996)); System.out.println(map.containsValue("东京")); */ /* Set<Integer> keys = map.keySet(); for(Integer key : keys){ String value = map.get(key); System.out.println(key+" --- "+value); } */ //print1(map); //print2(map); print3(map); } static <K, V> void print1(Map<K, V> map){ Set<K> keys = map.keySet(); for(K key : keys){ V value = map.get(key); System.out.println(key+" --- "+value); } } static <K, V> void print2(Map<K, V> map){ Collection<V> values = map.values(); for(V value : values){ System.out.println(value); } } static <K, V> void print3(Map<K, V> map){ Set<Map.Entry<K, V>> set = map.entrySet(); for(Map.Entry<K, V> me : set){ K key = me.getKey(); V value = me.getValue(); System.out.println(key+" --- "+value); } } }
-
运行结果:
21_JDK8中的Map遍历
-
forEach()
实现 BiConsumer接口 ,直接遍历Map -
代码(见上代码):
static <K, V> void print4(Map<K, V> map){ map.forEach(new BiConsumer<K, V>(){ public void accept(K key, V value){ System.out.println(key+" --- "+value); } }); }
22_Map的实现类
-
HashMap:
依靠 哈希算法 保证key不重复,JDK1.2、线程不安全、并发效率高、允许用null作为key或value -
Hashtable(注意t小写):
JDK1.0、线程安全、并发效率低、不允许用null作为key或value -
LinkedHashMap:
HashMap的子类,维护元素添加 到集合中的 顺序 -
TreeMap:
是 Map的子接口SortedMap的实现类,自动对key排序 -
Properties:
Hashtable的子类,key和value都是String,通常用于 配置文件的处理