Monday , December 11 2017
Home / java8 / Java 8 foreach Example Tutorials

Java 8 foreach Example Tutorials

In this tutorials, we are going to show how to use Java 8 foreach with lambda expressions. Usually we can use the foreach, while iterating the loops.

Java 8 foreach :

Java 8 introduced foreach as a method from 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&lt;Integer, String&gt; javaVersionMap) {

        javaVersionMap.forEach((k,v)-&gt;System.out.println(&quot;Version (&quot; + k + &quot;) : &quot; + v));

    }

    public static void main(String args[]) {
        Map&lt;Integer, String&gt; javaVersionMap = new HashMap&lt;Integer, String&gt;();
        javaVersionMap.put(1,
                &quot;The first version was released on January 23, 1996&quot;);
        javaVersionMap.put(2, &quot;Swing Graphical API and Collection Framework&quot;);
        javaVersionMap
                .put(3, &quot;Java Namining and Directory Interfaces included&quot;);
        javaVersionMap.put(4,
                &quot;assert keyword, Exception chaining and Regular Expressions &quot;);
        javaVersionMap.put(5,
                &quot;Generics, Enumarations, varargs and static imports&quot;);
        javaVersionMap.put(6, &quot;JDBC 4 Support and JAXB 2.0 upgradation&quot;);
        javaVersionMap.put(7,
                &quot;JVM Supports dynaminc languages, Stirngs in switch&quot;);
        javaVersionMap.put(8,
                &quot;Lambda expressions, Functional Programming and more..&quot;);
        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 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java 8 how to remove duplicates from list

In this tutorials, I am going to show you how to remove duplicate elements from …

Leave a Reply

Your email address will not be published. Required fields are marked *