java hashmap foreach lambda code example
Example 1: loop in java 8
List<Integer> data = new ArrayList<>();
for(int i = 0,size=data.size();i<size;i++){
System.out.println(data.get(i));
}
for(Integer element : data){
System.out.println(element);
}
data.forEach(System.out::println);
data.stream().forEach(System.out::println)
int i = 0;
while(i<data.size()){
System.out.println(data.get(i++));
}
Example 2: java 8 map foreach
public void iterateUsingStreamAPI(Map<String, Integer> map) {
map.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
}