标签:
Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复。
public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
LinkedHashMap例子:输出的结果为aa,bb,cc
public static void main(String[] args) { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
TreeMap例子:输出的结果为bb,aa,cc
public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(new Comparator<Object>(){ Collator collator = Collator.getInstance(); public int compare(Object o1, Object o2) { CollationKey key1 = collator.getCollationKey(o1.toString()); CollationKey key2 = collator.getCollationKey(o2.toString()); return key1.compareTo(key2); //return collator.compare(o1, o2); }}); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
HashMap,LinkedHashMap,TreeMap之间的区别
标签:
原文地址:http://www.cnblogs.com/hupp/p/4850260.html