LinkedHashSet
package chapter17.Set; import java.util.LinkedHashSet; import java.util.Set; public class LinkedHashSetSource { public static void main(String[] args) { Set set = new LinkedHashSet(); set.add(new String("AA"));//第一次添加时扩容到16 set.add(456); set.add(456);//无 set.add(new Customer("刘",1001)); set.add(123); set.add("ggg"); System.out.println(set);//有顺序 } } class Customer{ private String name; private int no; public Customer(String name, int no) { this.name = name; this.no = no; } }
package chapter17.Set; import java.util.LinkedHashSet; import java.util.Objects; public class LinkedHashSetExercise { public static void main(String[] args) { LinkedHashSet linkedHashSet = new LinkedHashSet(); boolean xx = linkedHashSet.add(new Car("xx", 1000)); boolean xx2 = linkedHashSet.add(new Car("xx", 1000));//无 boolean xx3 = linkedHashSet.add(new Car("y", 1000)); System.out.println(linkedHashSet); } } class Car{ private String name; private double price; public Car(String name, double price) { this.name = name; this.price = price; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Car car = (Car) o; return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name); } @Override public int hashCode() { return Objects.hash(name, price); } @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", price=" + price + '}'; } }
Map接口
package chapter17.Map; import com.sun.javafx.collections.MappingChange; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Map01 { public static void main(String[] args) { // Map 存放Key-Value存放双列元素 Map map = new HashMap(); map.put("1","xx"); map.put("2","yy"); System.out.println(map);//无序的,根据key的哈希值排序 map.put("1","zz"); System.out.println(map);//k相同时会替换 k-V都相同则无效 map.put(null,null); System.out.println(map);//K-V可以为空 System.out.println(map.get(null));//get方法返回key对应的value Set set = map.entrySet();//为方便遍历会创建EntrySet集合封装K-V Node<k,V>继承了Map.Entry 把HashMap里的Node存到entrySet,会有得到重要方法get K,V System.out.println(set.getClass()); System.out.println(set); for (Object o :set) { Map.Entry entry=(Map.Entry) o; System.out.println(entry.getKey()+"-"+entry.getValue()); } Set set1 =map.keySet();//封装K的组合 System.out.println(set1); Collection values = map.values(); System.out.println(values);//封装V的组合 } }
Map接口方法
package chapter17.Map; import java.util.HashMap; import java.util.Map; public class MapMethod { public static void main(String[] args) { Map map = new HashMap(); map.put("xx",new A("TT")); map.put("xx","yy");//替换 map.put("yy","zz"); System.out.println(map);//{xx=yy, yy=zz} System.out.println(map.get("xx"));//yy System.out.println(map.size());//2 System.out.println(map.isEmpty());//F System.out.println(map.containsKey("yy"));//T 有没有存在 map.clear();//清空 System.out.println(map);//{} } } class A{ private String name; public A(String name) { this.name = name; } }
Map六大遍历方式
package chapter17.Map; import java.util.*; //map六大遍历 public class MapFor { public static void main(String[] args) { Map map = new HashMap(); map.put("1","xx"); map.put("2","yy"); map.put("3",null); map.put("4","yy"); map.put(null,"yy"); //取出K 通过K取出对应V Set set = map.keySet(); System.out.println("==============");//1.增强for for (Object o :set) { System.out.println(o+"-"+map.get(o)); } System.out.println("==============");//2.迭代器 Iterator iterator=set.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); System.out.println(next+"-"+map.get(next)); } //直接取出values System.out.println("==============");//3.直接v 增强for Collection values = map.values(); for (Object o :values) { System.out.println(o); } System.out.println("==============");//4.直接v 迭代器 Iterator iterator1 = values.iterator(); while (iterator1.hasNext()) { Object next = iterator1.next(); System.out.println(next); } //用entryset获取k-v System.out.println("==============");//5.增强for Set set1 = map.entrySet(); for (Object o :set1) { Map.Entry m=(Map.Entry)o; System.out.println(m.getKey()+"-"+m.getValue()); } System.out.println("==============");//6.迭代器 Iterator iterator2 = set1.iterator(); while (iterator2.hasNext()) { Object next = iterator2.next();//运行类型是HashMap的Node 要转成MapEntry Map.Entry m=(Map.Entry)next; System.out.println(m.getKey()+"-"+m.getValue()); } } }
Map练习
package chapter17.Map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MapExercise { public static void main(String[] args) { HashMap hashMap = new HashMap(); hashMap.put("1",new Employee(1,"xx",100)); hashMap.put("2",new Employee(2,"yy",528527)); hashMap.put("3",new Employee(3,"zz",145247)); Set set1 = hashMap.keySet(); for (Object o :set1) { Employee employee = (Employee) hashMap.get(o);//向下转拿到sal if (employee.getSal()>18000){ System.out.println(employee); } } System.out.println("========="); Set set = hashMap.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry next = (Map.Entry)iterator.next();//next转型后可拿到Value对象 Employee m=(Employee)next.getValue();//对V对象转型拿到sal 而不是对next(entry)转型 if (m.getSal()>18000){ System.out.println(m); } } } } class Employee{ private int id; private String name; private double sal; public Employee(int id, String name, double sal) { this.id = id; this.name = name; this.sal = sal; } public double getSal() { return sal; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", sal=" + sal + '}'; } }
HMap小结
HMap底层机制
package chapter17.Map; import java.util.HashMap; public class HashMapSource { public static void main(String[] args) { HashMap map = new HashMap(); map.put("java",10); map.put("php",10); map.put("java",20);//修改 System.out.println(map); } }
HMap扩容树化触发
package chapte17.Map; import java.util.HashMap; //扩容树化触发 public class HashMapSource2 { public static void main(String[] args) { HashMap hashMap = new HashMap(); for (int i = 1; i <=12 ; i++) { hashMap.put(new A1(i),"hello"); } System.out.println(hashMap);//没有重写equals,有12个对象,是链表树化 } } class A1{ private int num; public A1(int num) { this.num = num; } //所有A对象hashcode都是100 @Override public int hashCode() { return 100; } @Override public String toString() { return "\nA1{" + "num=" + num + '}'; } }
Hashtable
package chapter17.Map; import java.util.Hashtable; public class HashTable01 { public static void main(String[] args) { Hashtable hashtable = new Hashtable(); hashtable.put("john",100); // hashtable.put(null,100);//异常 // hashtable.put("john",null);//异常 hashtable.put("lucy",100); hashtable.put("lic",100); hashtable.put("lic",88);//替换 System.out.println(hashtable); /* 1. 底层数组 Hashtable$Entry[] 初始化11个空间 2. 扩容临界值11*0.75=8 扩到23个 */ } }
properties
package chapter17.Map; import java.util.Properties; public class Properties01 { public static void main(String[] args) { Properties properties = new Properties(); //继承hashtable K-V存放 不能为null properties.put("xx",100); properties.put("yy",100); properties.put("zz",100); properties.put("zz",80);//换 System.out.println(properties);//{zz=80, xx=100, yy=100} System.out.println(properties.get("xx"));//100 } }
集合选型规则
TreeSet
package chapter17.Tree; import java.util.Comparator; import java.util.TreeSet; public class TreeSet01 { public static void main(String[] args) { // TreeSet treeSet = new TreeSet(); TreeSet treeSet =new TreeSet(new Comparator() { @Override public int compare(Object o1, Object o2) { // return ((String)o1).compareTo((String)o2);//字符串大小比较 return ((String)o1).length()-((String)o2).length();//字符串长度比较 一样则加不进 } }); treeSet.add("xx"); treeSet.add("yy"); treeSet.add("zz"); treeSet.add("dd"); System.out.println(treeSet);//当用无参构造器创建TreeSet 仍然是无序的 } }
TreeMap
package chapter17.Tree; import java.util.Comparator; import java.util.TreeMap; public class TreeMap01 { public static void main(String[] args) { // TreeMap treeMap = new TreeMap(); TreeMap treeMap = new TreeMap(new Comparator() { @Override public int compare(Object o1, Object o2) { return ((String)o1).compareTo((String)o2);//若比长度,长度一致也加不进 } }); treeMap.put("xx",55); treeMap.put("aa",56); treeMap.put("tt",57); treeMap.put("hhh",58); System.out.println(treeMap);//当用无参构造器创建TreeSet 仍然是无序的 } }
Collections工具类
package chapter17.Collection工具类; import java.util.*; public class Collections01 { public static void main(String[] args) { List list = new ArrayList(); list.add("xx"); list.add("yy"); list.add("zz"); list.add("ff"); Collections.reverse(list);//元素反转 System.out.println(list); Collections.shuffle(list);//随机打乱 System.out.println(list); Collections.sort(list);//自然排序 System.out.println(list); Collections.sort(list, new Comparator() {//按字符串长度 @Override public int compare(Object o1, Object o2) { return ((String)o1).length()-((String)o2).length(); } }); System.out.println(list); Collections.swap(list,0,1);//指定位置交换 System.out.println(list); System.out.println(Collections.max(list));//自然顺序最大元素 System.out.println(Collections.max(list, new Comparator() {//长度最大元素 @Override public int compare(Object o1, Object o2) { return ((String)o1).length()-((String)o2).length(); } })); System.out.println(Collections.frequency(list,"xx"));//出现频率 ArrayList dest = new ArrayList(); for (int i = 0; i <list.size() ; i++) {//两个size要一致,不然拷贝会报错 dest.add(""); } Collections.copy(dest,list);//list 复制到 dest System.out.println(dest); Collections.replaceAll(dest,"xx","XX");//替换元素 System.out.println(dest); } }