标签:
Map主要用于存储键值对, 根据键得到值, 所以不允许键重复, 但允许值重复
Mapmap1 = new HashMap(); map1.put("4", "Four"); map1.put("3", "Three"); map1.put("2", "Two"); map1.put("1", "One"); // 打印结果说明:HashMap不保证数据的插入顺序 System.out.println("HashMap :" + map1); //HashMap :{3=Three, 2=Two, 1=One, 4=Four}
Mapmap2 = new LinkedHashMap(); map2.put("4", "Four"); map2.put("3", "Three"); map2.put("2", "Two"); map2.put("1", "One"); // 打印结果说明:LinkedHashMap保证数据的插入顺序 System.out.println("LinkedHashMap :" + map2); //LinkedHashMap :{4=Four, 3=Three, 2=Two, 1=One}
Mapmap3 = new TreeMap(); map3.put("4", "Four"); map3.put("3", "Three"); map3.put("2", "Two"); map3.put("1", "One"); // 打印结果说明:TreeMap会根据Map的key值,进行从小到大的排序 System.out.println("TreeMap :" + map3); //TreeMap :{1=One, 2=Two, 3=Three, 4=Four}
标签:
原文地址:http://www.cnblogs.com/jin12/p/5611836.html