HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序

来源:互联网 发布:社交网络第二定律 编辑:程序博客网 时间:2024/05/17 21:58

HashMap:k,v可为null,LinkedHashMap:k,v可为null,TreeMap:v可为null,HashTable:k,v都不可为null,ConcurrentHashMap:k,v都不可为null,ConcurrentSkipListMap:k,v都不可为null。

HashMap,HashTable,ConcurrentHashMap 为无序输出;TreeMap为key排序输出;LinkedHashMap put顺序先后输;ConcurrentSkipListMap为key排序输出。

Map类型 KEY VALUE HashMap null null LinkedHashMap null null TreeMap not null null HashTable not null not null ConcurrentHashMap not null not null ConcurrentSkipListMap not null not null

下面代码测试:

public class MapKVIsNullTest {    public static void main(String[] args) {        // HashMap        System.out.println("------HashMap无序输出------");        HashMap<String, String> hashMap = new HashMap<String, String>();        hashMap.put("3", null);        hashMap.put("b", "Value1");        hashMap.put("2", "Value2");        hashMap.put("a", "ValueB");        hashMap.put("ee", "ValueA");        Iterator<Entry<String, String>> it = hashMap.entrySet().iterator();        while (it.hasNext()) {            Entry<String, String> e = it.next();            System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());        }        // TreeMap        System.out.println("------TreeMap按Key排序输出------");        TreeMap<String, String> teMap = new TreeMap<String, String>();        teMap.put("3", null);        teMap.put("1", "Value1");        teMap.put("2", "Value2");        teMap.put("b", "ValueB");        teMap.put("a", "ValueA");        Iterator<Entry<String, String>> tit = teMap.entrySet().iterator();        while (tit.hasNext()) {            Entry<String, String> e = tit.next();            System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());        }        // LinkedHashMap        System.out.println("--LinkedHashMap根据输入的顺序输出--");        LinkedHashMap<String, String> lhsMap = new LinkedHashMap<String, String>();        lhsMap.put(null, null); //        lhsMap.put("1", "Value1");        lhsMap.put("2", "Value2");        lhsMap.put("b", "ValueB");        lhsMap.put("a", "ValueA");        Iterator<Entry<String, String>> lit = lhsMap.entrySet().iterator();        while (lit.hasNext()) {            Entry<String, String> e = lit.next();            System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());        }        Collections.emptyMap();        // HashTable        System.out.println("--Hashtable无序输出--");        Hashtable<String, String> hstable = new Hashtable<String, String>();        hstable.put("3", "Value3");        hstable.put("1", "Value1");        hstable.put("2", "Value2");        hstable.put("b", "ValueB");        hstable.put("a", "ValueA");        Iterator<Entry<String, String>> ithstable = hstable.entrySet().iterator();        while (ithstable.hasNext()) {            Entry<String, String> e = ithstable.next();            System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());        }        // ConcurrentHashMap        System.out.println("--ConcurrentHashMap无序输出--");        ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();        concurrentHashMap.put("3", "Value3");        concurrentHashMap.put("b", "Value1");        concurrentHashMap.put("2", "Value2");        concurrentHashMap.put("a", "ValueB");        concurrentHashMap.put("ee", "ValueA");        for (Entry<String, String> v : concurrentHashMap.entrySet()) {            System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue());        }        // ConcurrentSkipListMap        System.out.println("--ConcurrentHashMap无序输出--");        ConcurrentSkipListMap<String, String> concurrentSkipListMap = new ConcurrentSkipListMap<String, String>();        concurrentSkipListMap.put("11", "Value3");        concurrentSkipListMap.put("b", "Value1");        concurrentSkipListMap.put("2", "Value2");        concurrentSkipListMap.put("a", "ValueB");        concurrentSkipListMap.put("ee", "ValueA");        for (Entry<String, String> v : concurrentSkipListMap.entrySet()) {            System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue());        }    }}
0 0