What is the easiest way to iterate over all the key/value pairs of a java.util.Map in Java 5 and higher?

前端 未结 1 503
甜味超标
甜味超标 2021-02-11 15:02

What is the easiest way to iterate over all the key/value pairs of a java.util.Map in Java 5 and higher?

1条回答
  •  灰色年华
    2021-02-11 15:48

    Assuming K is your key type and V is your value type:

    for (Map.Entry entry : map.entrySet()) {
      K key = entry.getKey();
      V value = entry.getValue();
      // do stuff
    }
    

    0 讨论(0)
提交回复
热议问题