HashMap、LinkedHashMap、TreeMap
1. hashMap
2. LinkedHashMap
上一个会记住下一个的地址
3. TreeMap
例:
1 import d3_genericity.set.Apple; 2 3 import java.util.Comparator; 4 import java.util.Map; 5 import java.util.TreeMap; 6 7 public class TreeMapDemo { 8 public static void main(String[] args) { 9 Map<Integer, String> maps1 = new TreeMap<>(); 10 11 maps1.put(22, "wl"); 12 maps1.put(23, "phx"); 13 maps1.put(2, "lr"); 14 System.out.println(maps1); 15 16 // TreeMap自带排序 17 Map<Apple, String> maps2 = new TreeMap<>(new Comparator<Apple>() { 18 @Override 19 public int compare(Apple o1, Apple o2) { 20 // 降序, 因为价钱没有相等的,所以不会覆盖 21 return Double.compare(o2.getPrice(), o1.getPrice()); 22 } 23 }); 24 maps2.put(new Apple("红富士", "红色", 15.2, 500), "上海"); 25 maps2.put(new Apple("青苹果", "绿色", 9.9, 600), "重庆"); 26 maps2.put(new Apple("白苹果", "白色", 8.8, 600), "四川"); 27 28 System.out.println(maps2); 29 } 30 }