The forEach statement in Java8 loops an List and an Map
- 2021-08-31 07:44:38
- OfStack
In this article, I'll show you how to loop an List and an Map with the new Java 8 forEach statement.
1. forEach and Map
1.1. The common method of circulating Map.
Map<String ,Integer> items = new HashMap<>();
items.put("A",10);
items.put("B",20);
items.put("C",30);
items.put("D",40);
items.put("E",50);
items.put("F",60);
for(Map.Entry<String Integer> entry : items.entrySet()){
System.out.println("Item : "+enty.getKey() + " Count :" + entry.getVlaue())
}
1.2 In Java 8, you can loop Map with the forEach + lambda expression
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println(" Item : "+ k + " Count : " + v));
//items.forEach((k,v)->{System.out.println(" Item : "+ k + " Count : " + v);});
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});
2. forEach and List
2.1. Conventional method of circulating List
List<String> items = new ArrayList<>();
items.add("Anna");
items.add("Brian");
items.add("Cici");
items.add("Elena");
// Regular cycle List Method of
for(String item : items){
System.out.println(item);
}
2.2. In Java 8, you can use forEach + lambda expressions or method calls to loop Map
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
// In Java 8 In, You can use forEach + lambda Expression to loop Map
// Output above put All lists of
items.forEach((k,v)->System.out.println(" Item : "+ k + " Count : " + v));
//items.forEach((k,v)->{System.out.println(" Item : "+ k + " Count : " + v);});
// Output above put All lists of + Hello E
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});
Map
package com.foreach.learn;
import java.util.HashMap;
import java.util.Map;
public class forEachLearnMap {
public static void main(String[] args) {
//forEach And Map
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
// Regular cycle Map Method of
for(Map.Entry<String, Integer> entry : items.entrySet()) {
System.out.println(" Item : "+ entry.getKey() + " Count : " + entry.getValue());
}
// In Java 8 In, You can use forEach + lambda Expression to loop Map
// Output above put All lists of
items.forEach((k,v)->System.out.println(" Item : "+ k + " Count : " + v));
//items.forEach((k,v)->{System.out.println(" Item : "+ k + " Count : " + v);});
// Output above put All lists of + Hello E
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});
}
}
Lsit:
package com.foreach.learn;
import java.util.ArrayList;
import java.util.List;
public class forEachLearnList {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Anna");
items.add("Brian");
items.add("Cici");
items.add("Elena");
// Regular cycle List Method of
for(String item : items){
System.out.println(item);
}
// In Java 8 In, You can use forEach + lambda Expression Or method calls to loop Map
//lambda
// Output : Anna, Brian, Cici, Elena
items.forEach(item->System.out.println(item));
// Output Cici
items.forEach(item->{
if("Cici".equals(item)){
System.out.println(item);
}
});
// Method invocation
// Output : Anna, Brian, Cici, Elena
items.forEach(System.out::println);
//Stream and filter
// Output Brian
items.stream()
.filter(s->s.contains("Brian"))
.forEach(System.out::println);
}
}