一、未指定排序方式时
HashMap通过计算存入键值对的Key值进行排序
HashTree通过对存入的键值对的Key值进行升序排序
public static void main(String[] args){
Map<String,Integer> map = new HashMap<String, Integer>();
System.out.println("hashmap输出");
mapadd(map);
Map<String,Integer> map2 = new TreeMap<String, Integer>();
System.out.println("treemap输出");
mapadd(map2);
}
private static void mapadd(Map<String, Integer> map) {
map.put("a",1);
map.put("d",4);
map.put("c",3);
map.put("b",2);
for (Map.Entry<String,Integer> me:
map.entrySet()) {
System.out.println(me.getKey()+" "+me.getValue()