Pregunta

I am using processing, and I have a HashMap, in which I want to be able to increment all the values by 1. The code looks like this:

HashMap<Character, Integer> keyStates = new HashMap<Character, Integer>();

void press(Character k) {
  keyStates.put(k, 1);
}

void release(Character k) {
  keyStates.put(k, 0);
}

And the Integer tells me how long I have been pressing a key. Thus, I want to increment all the values of this HashMap by one, regardless of key, every "tick" or frame. Is there a way to increment all Integer Values of a HashMap, or, if not, another way of getting around this issue. Thank you very much for your help. Also, this is my first post, so please tell me if I am doing it right.

¿Fue útil?

Solución

The best way to do it would be by using Iterator :

  Iterator<Map.Entry <Character, Integer> > it = keyStates.entrySet().iterator();
  while (it.hasNext()) {
      Map.Entry<Character, Integer> pair = it.next();
      Integer newCount = (pair.getValue() == null) ? 1 : pair.getValue() + 1 ;
      pair.setValue(newCount);
  }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top