Java 和 HashMap - 如何添加到现有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21371447/
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
Java and HashMap - how to add to existing values?
提问by Pink Jazz
I am currently working on a client/server application, and I am using a HashMap to identify each client. The client passes in two values to the server - id
and x
. I am using id
as my key, and for each element in the HashMap, I would like to use the value of x
to add to the value the specific element with the matching key id
.
我目前正在开发客户端/服务器应用程序,我正在使用 HashMap 来标识每个客户端。客户端将两个值传递给服务器 -id
和x
。我id
用作我的键,对于 HashMap 中的每个元素,我想使用 的值x
将具有匹配键的特定元素添加到值中id
。
I have the following code:
我有以下代码:
HashMap<Integer, Integer> clients = new HashMap<Integer, Integer>();
int x;
sock = serv.accept();
in = new ObjectInputStream(sock.getInputStream());
in2 = sock.getInputStream();
out = new ObjectOutputStream(sock.getOutputStream());
int id = in2.read();
System.out.println("id = " + id);
Object o = in.readObject();
System.out.println("Server received " + o);
if (o instanceof Integer) {
x = ((Integer) o).intValue();
clients.put(id, 0 + x); //doesn't work, I need to be able to add to the existing value instead of overwriting the value
} else if (o instanceof String) {
clients.put(id, 0);
}
out.writeObject(clients.get(id));
out.flush();
Could anyone help me here?
有人可以在这里帮助我吗?
采纳答案by chrylis -cautiouslyoptimistic-
Integer previousValue = clients.get(id);
if(previousValue == null) previousValue = 0;
clients.put(id, previousValue + x);
回答by Prince
You can do something like this:
你可以这样做:
Integer value = new Integer(0);
if (clients.containsKey(id)) {
value = clients.get(id);
}
value.add(new Integer(x) + value);
clients.put(id, value);
回答by Jigar Joshi
You need to read value from map then you can add it something like
您需要从地图中读取值,然后您可以添加类似
Integer resultantValue = x;
Integer previousValue = clents.get(id);
if(previousValue!=null){
resultantValue+=previousValue;
}
clients.put(id, resultantValue);
回答by Loktopus
The best way to do this is probably to create an extension of HashMap specifically for integers with a method specifically for adding to a particular value in the manner you specified, like this:
最好的方法可能是创建一个专门用于整数的 HashMap 扩展,其中包含一个专门用于以您指定的方式添加特定值的方法,如下所示:
import java.util.HashMap;
public class IntegerHashMap extends HashMap<Integer,Integer>{
public void addValueToKey(Integer key, Integer valueToAdd){
Integer originalValue = this.get(key);
Integer newValue = originalValue + valueToAdd;
this.put(key, newValue);
}
}
This will make it easy to use the code again if needed, which by the looks of it, it very well could be, and has the added benefit of working only with a HashMap of the correct type.
如果需要,这将使再次使用代码变得容易,从它的外观来看,它很可能是,并且具有仅使用正确类型的 HashMap 的额外好处。
回答by Derked
In Java 8 or above you can use Merge.
在 Java 8 或更高版本中,您可以使用 Merge。
int newValue = 3;
clients.merge(id, newValue, Integer::sum);