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

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类型KEYVALUE
HashMapnullnull
LinkedHashMapnullnull
TreeMapnot nullnull
HashTablenot nullnot null
ConcurrentHashMapnot nullnot null
ConcurrentSkipListMapnot nullnot 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值