java中关于Map.Entry和entrySet()方法的使用说明

//jdk版本为1.8
public static void main(String[] args) {
    Map map = new HashMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    for (Object key : map.keySet()) {
      System.out.println(key);
    }

    Set> set = map.entrySet();

    set.forEach(
        s -> {
          System.out.println(s.getValue());
          System.out.println(s.getKey());
        });

    map.forEach(
        (k, v) -> {
          System.out.println("key: " + k + ", value:" + v);
        });
  }

输出如下:

key1
key2
key3
value1
key1
value2
key2
value3
key3
key: key1, value:value1
key: key2, value:value2
key: key3, value:value3

你可能感兴趣的:(java中关于Map.Entry和entrySet()方法的使用说明)