Java Map foreach Example - Coded Java

Latest

Quality & Simple Java Content

Dec 31, 2017

Java Map foreach Example

Java Map foreach Example

Java Map foreach Example

Map

  Map<Integer, String> colourMap = new HashMap<>();
  colourMap.put(1, "RED");
  colourMap.put(2, "GREEN");
  colourMap.put(3, "BLUE");
  colourMap.put(4, "YELLOW");
  colourMap.put(5, "BLACK");
  colourMap.put(6, "WHITE");

  for (Map.Entry<Integer, String> color : colourMap.entrySet()){
System.out.println( color.getKey() + " -> " + color.getValue());
  }

Java 8 Lambdas

  colourMap.forEach((k, v) -> System.out.println((k + " -> " + v)));


Reference



https://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

No comments:

Post a Comment