java.util.Map的putIfAbsent方法

其他网站描述是这个样子的

putIfAbsent() 方法会先判断指定的键(key)是否存在,不存在则将键/值对插入到 HashMap 中。

为了让人能够理解,下面放出代码

        Map<String, String> test = new HashMap<String, String>();
        test.put("1",null);
        System.out.println(test);
        test.putIfAbsent("1","123");
        System.out.println(test);
        test.putIfAbsent("2","44444");
        System.out.println(test);

输出内容:

{1=null}
{1=123}
{1=123, 2=44444}


版权声明:本文为yuliqi0429原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。