Java 8 foreach :
Java 8 introduced foreach as a method from the Iterable interface, which performs the given action for the Iterable until all elements have been processed.
This tutorials is about how to use foreach in Java 8 when we are looping the List and Map. And also how to filter the list items using Stream.filter().
Java 8 foreach for List :
In the previous versions of Java, we used to loop the list of items using foreach loop like below.
List<String> list = new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("Hibernate");
list.add("JQuery");
list.add("AngularJs");
list.add("Java 8 Foreach");
for (String cources : list) {
System.out.println(cources);
}
From Java8, we can loop the same thing with lambda expressions like below.
list.forEach(cource -> System.out.println(cource));
foreach with filter :
We can apply the filters for the list items, while iterating the foreach in Java 8.
list.stream().filter(cources -> cources.contains("Java 8 Foreach"))
.forEach(System.out::println);
Example for Java 8 foreach :
package com.onlinetutorialspoint.java8;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Java8_Foreach {
public static void normalLoop(List<String> list) {
for (String cources : list) {
System.out.println(cources);
}
}
public static void java8Loop_Lambda(List<String> list) {
list.forEach(cource -> System.out.println(cource));
}
public static void java8Loop_MethodReference(List<String> list) {
list.forEach(System.out::println);
}
public static void java8Loop_Stream_Filter(List<String> list) {
list.stream().filter(cources -> cources.contains("Java 8 Foreach")).forEach(System.out::println);
}
public static void main(String args[]) {
List<String> list = new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("Hibernate");
list.add("JQuery");
list.add("AngularJs");
list.add("Java 8 Foreach");
java8Loop_Stream_Filter(list);
}
}
Java 8 Foreach
foreach with Map :
We have discussed in the previous tutorials, how to iterate the map in java in previous versions. Now this is the time to loop over the Map using Java 8 foreach method.
Legacy way to loop the Map :
for (Map.Entry<Integer, String> entry : javaVersionMap.entrySet()) {
System.out.println("Version (" + entry.getKey() + ") : " + entry.getValue());
}
foreach using lambda in Map:
for (Map.Entry<Integer, String> entry : javaVersionMap.entrySet()) {
System.out.println("Version (" + entry.getKey() + ") : " + entry.getValue());
}
foreach using lambda in Map Example :
public class Java8_ForeachMap {
public static void java8Loop_Lambda(Map<Integer, String> javaVersionMap) {
javaVersionMap.forEach((k, v) -> System.out.println("Version (" + k + ") : " + v));
}
public static void main(String args[]) {
Map<Integer, String> javaVersionMap = new HashMap<Integer, String>();
javaVersionMap.put(1, "The first version was released on January 23, 1996");
javaVersionMap.put(2, "Swing Graphical API and Collection Framework");
javaVersionMap.put(3, "Java Namining and Directory Interfaces included");
javaVersionMap.put(4, "assert keyword, Exception chaining and Regular Expressions ");
javaVersionMap.put(5, "Generics, Enumarations, varargs and static imports");
javaVersionMap.put(6, "JDBC 4 Support and JAXB 2.0 upgradation");
javaVersionMap.put(7, "JVM Supports dynaminc languages, Stirngs in switch");
javaVersionMap.put(8, "Lambda expressions, Functional Programming and more..");
java8Loop_Lambda(javaVersionMap);
}
}
Version (1) : The first version was released on January 23, 1996
Version (2) : Swing Graphical API and Collection Framework
Version (3) : Java Namining and Directory Interfaces included
Version (4) : assert keyword, Exception chaining and Regular Expressions
Version (5) : Generics, Enumarations, varargs and static imports
Version (6) : JDBC 4 Support and JAXB 2.0 upgradation
Version (7) : JVM Supports dynaminc languages, Stirngs in switch
Version (8) : Lambda expressions, Functional Programming and more..
Happy Learning 🙂