Map的remove()方法

测试代码如下: 

    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("aa", "ssssss");
        map.put("bb", "cccccc");
        map.put("cc", "ffffff");
        map.put("dd", "gggggg");
        map.put("ee", "kkkkkk");

        String contaions = "aa";
        System.out.println("before: contains " + contaions + " : " + map.containsKey(contaions));
        System.out.println("before: map = " + JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("remove: " + contaions);
        map.remove(contaions);
        System.out.println("after remove: contains " + contaions + " : " + map.containsKey(contaions));
        System.out.println("after remove: map = " + JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("put null: " + contaions);
        map.put(contaions, null);
        System.out.println("after put null: contains " + contaions + " : " + map.containsKey(contaions));
        System.out.println("after put null: map = " + JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));

    }

结果如下:

before: contains aa : true
before: map = {"aa":"ssssss","bb":"cccccc","cc":"ffffff","dd":"gggggg","ee":"kkkkkk"}
----------------------------------------------------------------------------
remove: aa
after remove: contains aa : false
after remove: map = {"bb":"cccccc","cc":"ffffff","dd":"gggggg","ee":"kkkkkk"}
----------------------------------------------------------------------------
put null: aa
after put null: contains aa : true
after put null: map = {"bb":"cccccc","cc":"ffffff","dd":"gggggg","ee":"kkkkkk","aa":null}

 

你可能感兴趣的:(java开发)