map排序及keyset顺序问题
作者:互联网
package test; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class testKeySetSort { public static void main(String[] args) { System.out.println("## HashTable --降序 ##"); Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("c1","000"); ht.put("a3","000"); ht.put("c2","000"); ht.put("c5","000"); ht.put("c4","000"); Iterator<String> it = ht.keySet().iterator(); while(it.hasNext()) { System.out.println(it.next()); } System.out.println("## TreeMap --升序 ##"); TreeMap<String,String> tm = new TreeMap<String,String>(); tm.put("c1","000"); tm.put("a3","000"); tm.put("c2","000"); tm.put("c5","000"); tm.put("c4","000"); Iterator<String> it2 = tm.keySet().iterator(); while(it2.hasNext()) { System.out.println(it2.next()); } System.out.println("## HashMap --乱序 ##"); Map<String, String> hm = new HashMap<String, String>(); hm.put("c1","000"); hm.put("a3","000"); hm.put("c2","000"); hm.put("c5","000"); hm.put("c4","000"); Iterator<String> it3 = hm.keySet().iterator(); while(it3.hasNext()) { System.out.println(it3.next()); } System.out.println("## LinkedHashMap --原序 ##"); LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>(); lhm.put("c1","000"); lhm.put("a3","000"); lhm.put("c2","000"); lhm.put("c5","000"); lhm.put("c4","000"); Iterator<String> it4 = lhm.keySet().iterator(); while(it4.hasNext()) { System.out.println(it4.next()); } } }View Code
输出:
标签:map,##,System,keyset,000,put,println,排序,out 来源: https://www.cnblogs.com/daytoy105/p/16654825.html