简体   繁体   English

如何在不覆盖以前的数据的情况下使用 hashmap.put? (爪哇)

[英]How to use hashmap.put without overriding the previous data? (java)

I have something along the lines of this:我有一些类似的东西:

public HashMap<Boolean, String> map = new HashMap();
map.put(this.inverted, "Inverted");
map.put(this.active, "Loading");
System.out.println(map.size());

after seeing that the size was always 1, I realised that using map.put was overriding the previous data.在看到大小始终为 1 后,我意识到使用 map.put 覆盖了以前的数据。 I am currently trying to iterate over the hashmap.我目前正在尝试遍历哈希图。 Is there a way to add mappings to it without overriding previous ones?有没有办法在不覆盖以前的映射的情况下向它添加映射?

You have declared your HashMap as: -您已将HashMap声明为:-

public HashMap<Boolean, String> map = new HashMap();

Now, just think how many maximum mapping can you have in your map ?现在,想想你的map可以有多少个最大映射? The answer you can get by thinking of, what all values can your Boolean type take.您可以通过思考得到的答案是,您的Boolean类型可以采用什么值。 This is because, you cannot have duplicate keys in a HashMap .这是因为,在HashMap不能有重复的键。

So, probably you got it now, that you can at max have only 2 mappings in your map, one for true and other for false (In fact you can have a 3rd one too, as you can have a mapping for a null key too in your HashMap ).所以,可能你现在明白了,你的2 mappings中最多只能有2 mappings ,一个是true ,另一个是false (事实上​​你也可以有3rd个,因为你也可以有一个null键的映射)在您的HashMap )。

So, in your case, if both this.inverted and this.active are either true or false .因此,在您的情况下,如果this.invertedthis.active都是truefalse Then only one of them can be there, and that would be the later value inserted.那么只有其中一个可以在那里,这将是后来插入的值。

Is there a way to add mappings to it without overriding previous ones?有没有办法在不覆盖以前的映射的情况下向它添加映射?

Probably you have build your HashMap wrongly.可能您错误地构建了HashMap You should declare your map as: -您应该将地图声明为:-

private Map<String, Boolean> map = new HashMap();

And now you can put two mappings as: -现在你可以把两个映射作为: -

map.put("Inverted", this.inverted);
map.put("Loading", this.active);

It's because this.inverted.equals(this.active) and this.inverted.hashcode()==this.active.hashcode()这是因为this.inverted.equals(this.active)this.inverted.hashcode()==this.active.hashcode()

Maybe you need redefine the equals method for the key.也许您需要为键重新定义 equals 方法。

In MAP在地图中

An object that maps keys to values.将键映射到值的对象。 A map cannot contain duplicate keys;地图不能包含重复的键; each key can map to at most one value.每个键最多可以映射到一个值。 ---> fromMap Api ---> 来自Map Api

from your implementation, may be this.inverted and this.active both have same value.从您的实现this.inverted ,可能this.invertedthis.active都具有相同的值。

Check the input once.检查输入一次。 print the keySet, then check.打印 keySet,然后检查。

or change the input to Map<String, Boolean>或将输入更改为Map<String, Boolean>

As @Frank suggest you should invert your Map.正如@Frank 建议你应该反转你的地图。

public final Map<String, Boolean> map = new HashMap<>();

map.put("Inverted", this.inverted);
map.put("Loading", this.active);
System.out.println(map);

If the keys are the same than the previous value is overwritten in a standard Java Map.如果键与之前的值相同,则在标准 Java Map 中被覆盖。 If you don't want this, you can have a look at a multimap which is implemented for example in commons-collections.如果你不想要这个,你可以看看在 commons-collections 中实现的 multimap。 It can hold different values for one key.它可以为一个键保存不同的值。

Hashmap is based on key/value pairs. Hashmap 基于键/值对。 If your keys are equal (they have the same hashcode), it will behave as you described.如果您的键相等(它们具有相同的哈希码),它将按照您的描述运行。

For your use case, reversing your key/value pairs will help you.对于您的用例,反转键/值对将对您有所帮助。

public HashMap<String, Boolean> map = new HashMap();
map.put("Inverted", this.inverted); 
map.put("Loading", this.active);
System.out.println(map.size());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM