java 如何更新地图中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:43:24  来源:igfitidea点击:

How can I update values in a map?

javacollections

提问by JJunior

I am using a TreeMap<Integer,Object>()to store values.
Now my Objecthas a component, Object.value(), which keeps getting incremented as per values read from a file.
So, I evaluate whether the key exists and need to update value.
I do not understand how I can update values in a Mapin Java.
I cannot just replace the whole record, as new values need to be added to the existing record value.
Is there a better way to do this than using a map? I used a map because I will keep searching for the keys.
Any suggestions?

我正在使用 aTreeMap<Integer,Object>()来存储值。
现在我Object有一个组件,Object.value(),它根据从文件中读取的值不断增加。
因此,我评估密钥是否存在并需要更新值。
我不明白如何Map在 Java 中更新 a中的值。
我不能只替换整个记录,因为需要将新值添加到现有记录值中。
有没有比使用地图更好的方法来做到这一点?我使用了地图,因为我会继续寻找钥匙。
有什么建议?

回答by shoebox639

Well using a map is correct if you want to be able to quickly access your key-value pairs. If you're values are just MyObjectswith a .value(), can't you get the object and reset that value?

如果您希望能够快速访问您的键值对,那么使用地图是正确的。如果您的值只有MyObjectsa .value(),您不能获取对象并重置该值吗?

MyObject myObj = treeMap.get(key);
myObj.setValue(myObj.getValue()++);

I'm using MyObject here as the poster was using Objectto denote an example type.

我在这里使用 MyObject 作为海报Object用来表示示例类型。

回答by Waldheinz

Your "object" needs to have an setter which updates the value. So you just retrieve the object in question from the map, call the setter on this object, et voila. The only obstacle you must take care of is that whatever you do in your setXXXmethod does not alter the outcoming of the equalsand hashCodemethods, as this violates the invariants implied by the TreeMapand will result in unpredictable behavior. You Object might look like this:

您的“对象”需要有一个更新值的设置器。所以你只需从地图中检索有问题的对象,在这个对象上调用 setter,等等。您必须注意的唯一障碍是,无论您在setXXX方法中做什么,都不会改变equalshashCode方法的结果,因为这违反了 隐含的不变量TreeMap,并将导致不可预测的行为。您的对象可能如下所示:

class AnObject {
   private int cnt;
   public void increment() { this.cnt++ };
}

You can pull it out of the TreeMap, call increment()and do not have to alter the contents of the TreeMapitself.

您可以将其从TreeMap, 调用中拉出increment()并且不必更改其TreeMap本身的内容。

回答by Hal

I'm not sure of what you are trying to do but if you just want to store objects with keys you should use a Hashtable. It allows to map keys to objects.

我不确定您要做什么,但如果您只想存储带有键的对象,您应该使用哈希表。它允许将键映射到对象。

//create a hashtable
//the first template type is the type of keys you want to use
//the second is the type of objects you store (your Object)
Hashtable <Integer,MyObject> myHashtable = new Hashtable <Integer,MyObject> ();

//Now you create your object, and set one of its fields to 4.
MyObject obj = new MyObject();
obj.setValue(4);

//You insert the object into the hashtable, with the key 0.
myHashtable.put(0,obj);

//Now if you want to change the value of an object in the hashtable, you have to retrieve it from its key, change the value by yourself then re-insert the object into the hashtable.
MyObject obj2 = myHashtable.get(0);

obj.setValue(obj.getValue() + 2);

//This will erase the previous object mapped with the key 0.
myHashtable.put(0,obj);

Hope this helps.

希望这可以帮助。