且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java-如何从HashMap中删除重复项?

更新时间:2022-04-30 21:38:42

不需要这样做,HashMap会自动进行处理.当执行该代码时,基本上会发生以下情况:

There is no need to do that, HashMap takes care of that automatically. What happens when you execute that code is essentially as follows:

meh.put("one", 1);

这使得地图为{"one"-> 1}

this makes the map {"one" -> 1}

meh.put("one", 1);

这将自己替换分配,使映射为{"one"-> 1}

this replaces the assignment by itself, making the map {"one" -> 1}

meh.put("one", 1);

这将自己替换分配,使映射为{"one"-> 1}

this replaces the assignment by itself, making the map {"one" -> 1}

meh.put("two", 1);

这将添加请求的链接,从而使映射为{"one"-> 1,"two"-> 1}

this adds the requested linking, making the map {"one" -> 1, "two" -> 1}

meh.put("two", 2);

这将替换为两个"的分配,从而使映射为{一个"-> 1,两个"-> 2}

this replaces the assignment for "two", making the map {"one" -> 1, "two" -> 2}

meh.put("three", 3);

这将添加新元素,从而使总映射为{一个"-> 1,两个"-> 2,三个"-> 3},而没有重复项.

this adds the new element, making the total mapping {"one" -> 1, "two" -> 2, "three" -> 3} with no duplicates involved.