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
How can I update values in a map?
提问by JJunior
I am using a TreeMap<Integer,Object>()
to store values.
Now my Object
has 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 Map
in 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 MyObjects
with a .value()
, can't you get the object and reset that value?
如果您希望能够快速访问您的键值对,那么使用地图是正确的。如果您的值只有MyObjects
a .value()
,您不能获取对象并重置该值吗?
MyObject myObj = treeMap.get(key);
myObj.setValue(myObj.getValue()++);
I'm using MyObject here as the poster was using Object
to 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 setXXX
method does not alter the outcoming of the equals
and hashCode
methods, as this violates the invariants implied by the TreeMap
and will result in unpredictable behavior. You Object might look like this:
您的“对象”需要有一个更新值的设置器。所以你只需从地图中检索有问题的对象,在这个对象上调用 setter,等等。您必须注意的唯一障碍是,无论您在setXXX
方法中做什么,都不会改变equals
和hashCode
方法的结果,因为这违反了 隐含的不变量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 TreeMap
itself.
您可以将其从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.
希望这可以帮助。