title: Java and HashMap - how to add to existing values?
body: <p>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 - <code>id</code> and <code>x</code>.  I am using <code>id</code> as my key, and for each element in the HashMap, I would like to use the value of <code>x</code> to add to the value the specific element with the matching key <code>id</code>.  </p>
  
  <p>I have the following code:</p>
  
  <pre><code>HashMap&lt;Integer, Integer&gt; clients = new HashMap&lt;Integer, Integer&gt;();
  
  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();
  </code></pre>
  
  <p>Could anyone help me here?</p>