Java Properties的null值

环境

  • Ubuntu 22.04
  • Java 17.0.3.1

HashMap和ConcurrentHashMap

HashMap

        Map map1 = new HashMap();
        map1.put("key1", "value1");
        map1.put("key2", "value2");
        map1.put("key3", "");
        map1.put("key4", null);

        System.out.println(map1);

运行结果如下:

{key1=value1, key2=value2, key3=, key4=null}

ConcurrentHashMap

        Map map1 = new ConcurrentHashMap();
        map1.put("key1", "value1");
        map1.put("key2", "value2");
        map1.put("key3", "");
        map1.put("key4", null); // 此处抛出异常

        System.out.println(map1);

报错如下:

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
	at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
	at pck1.Test0824.main(Test0824.java:28)

查看 ConcurrentHashMapput() 方法,如下:

    /**
     * Maps the specified key to the specified value in this table.
     * Neither the key nor the value can be null.
     *
     * <p>The value can be retrieved by calling the {@code get} method
     * with a key that is equal to the original key.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with {@code key}, or
     *         {@code null} if there was no mapping for {@code key}
     * @throws NullPointerException if the specified key or value is null
     */
    public V put(K key, V value) {
        return putVal(key, value, false);
    }

keyvalue 上都有 @NotNull 注解,且注释里也提到了,如果 key 或者 valuenull ,就会抛出 NullPointerException

再查看 putVal() 方法,如下:

    final V putVal(K key, V value, boolean onlyIfAbsent) {
        if (key == null || value == null) throw new NullPointerException();
    ......

总结

HashMap 可以插入null值,但是 ConcurrentHashMap 不能插入null值,否则会抛NPE异常。

Properties

插入

        Properties p = new Properties();
        p.put("key1", "value1");
        p.put("key2", "value2");
        p.put("key3", "");
//        p.put("key4", null);

        System.out.println(p);

        Object obj = p.getProperty("key3");

        System.out.println(obj == null ? "null" : "not null");

运行结果如下:

{key1=value1, key2=value2, key3=}
not null

其中, key3 的值是空字符串( "" ),而不是null。

如果试图插入null值(将上面代码中的 key4 所在行反注释),则报错如下:

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
	at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
	at java.base/java.util.Properties.put(Properties.java:1301)
	at pck1.Test0824.main(Test0824.java:14)

从调用栈可见, Properties 使用的是 ConcurrentHashMap ,所以,显然无法插入null值。

读取

创建文件 test.properties 如下:

key1=value1
key2=value2
key3=

读取 test.properties 文件:

        Properties properties = new Properties();
        InputStream in = Test0824.class.getClassLoader().getResourceAsStream("pck1/test.properties");
        try {
            properties.load(in);

            String value1 = properties.getProperty("key1");
            System.out.println(value1);

            String value2 = properties.getProperty("key2");
            System.out.println(value2);

            String value3 = properties.getProperty("key3");
            System.out.println(value3);
            System.out.println(value3 == null); // false
            System.out.println(value3.equals("")); // true

            String value4 = properties.getProperty("key4");
            System.out.println(value4);
            System.out.println(value4 == null); // true

        } finally {
            in.close();
        }

运行结果如下:

value1
value2

false
true
null
true

可见,对于 key3= 这样的配置,读取的value是空字符串( "" ),而不是null值。而对于不存在的key值(比如本例中的 key4 ),读取的value是null值。

反过来想,之所以Properties里除非是不存在的key值,否则读取和插入都不存在null值的情况,也许目的正是为了区分key值是否存在吧。

总结

只有读取不存在的key值时,才会出现null值,否则无论读取或插入,都不存在value为null值的情况(读取为空字符串,插入报错NPE)。

具体如下:

  • 对于 key= 这样的配置,读取的value是空字符串( "" );
  • 对于不存在的key值,读取的value是null值;
  • 插入时,不允许value为null值,否则抛出NPE异常;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值