zoukankan      html  css  js  c++  java
  • java集合 list、set、map能否存储null

    java集合能否存储null

    package com.idea.test.nulltest;
    
    import jxl.common.Assert;
    
    import java.util.*;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class NullTest {
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add(null);
            list.add(null);
            System.out.println("ArrayList :"+list.size());   //2
    
    
            List linkedList = new LinkedList();
            linkedList.add(null);
            linkedList.add(null);
            System.out.println("LinkedList :"+linkedList.size());  //2
    
    
    
            Set set=new HashSet();
            set.add(null);
            set.add(null);
           System.out.println("HashSet :"+set.size());// 1
    
            Set linkedHashSet=new LinkedHashSet();
            linkedHashSet.add(null);
            linkedHashSet.add(null);
            System.out.println("LinkedHashSet :"+linkedHashSet.size()); //1
    
            Set treeSet=new TreeSet();
            //treeSet.add(null);   //treeSet key不能null ava.lang.NullPointerException
            //treeSet.add(null);
           // System.out.println("TreeSet : "+treeSet.size());
    
            HashMap<String,String> map = new HashMap<>();
            map.put(null,null);
            map.put("liubei",null);
            System.out.println("HashMap :"+map.size());// 2
    
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put(null,null);
            linkedHashMap.put("liubei",null);
            System.out.println("LinkedHashMap :"+linkedHashMap.size()); //2
    
    
    
            TreeMap treeMap= new TreeMap<>();
            treeMap.put("liu",null);   //treeMap key  不能为空,value 可以为空
            //treeMap.put(null,"liubei");
            //treeMap.put(null,null);
            System.out.println("TreeMap :"+treeMap.size());
    
    
            Hashtable table=new Hashtable();
            //table.put("liu",null);  
            //table.put(null,"关羽");    //hashtable key与value均不能为空
            table.put(null,null);
            System.out.println("Hashtable"+table.size());
    
    
            ConcurrentHashMap concurrentHashMap=new ConcurrentHashMap();
            //concurrentHashMap.put("liu",null);    //concurrentHashmap key 与 value 均不可以为空
            //concurrentHashMap.put(null,"zhangfei");
            System.out.println("ConcurrentHashMap :"+concurrentHashMap.size());
        }
    
    }
    
    
    
    

    综上所述可以得出结论:

    1、vector、arraylist、linkedlist可以存储多个null.

    2、hashset、linkedset可以存储一个null. treeset不能存储null.

    3、hashmap 、linkedhashmap  key与value均可以为null.  treemap  key不可以为null,value可以为null.  hashtable、concurrenthashmap key与value均不能为null.

  • 相关阅读:
    centos7 关闭firewall安装iptables并配置
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    URL地址下载图片到本地
    IDEA常用快捷键
    电商的支付前、中、后这3个流程都是怎么设计的?
    jenkins的部署
    mysql 授权用户 主从和备份
    windows下利用iis建立网站网站并实现局域共享
    nginx反向代理 和部分优化
    LNMP的搭建 及地址转换
  • 原文地址:https://www.cnblogs.com/zouhong/p/13566576.html
Copyright © 2011-2022 走看看