java 8 forEach Loop Example using List and Map

forEach - 

forEach is available from java 8  and it is present in  Iterable interface (Interface Iterable<T> in java.lang package) and Stream Interface (Interface Stream<T>  in java.util.stream package).
By using forEach loop we are able to iterate over the collection.

Method Details -

void forEach(Consumer<? super T> action)
*  Performs an action for each element of this stream.
* This is a terminal operation.

(I) forEach And List - 

* Before java 8 normal way to loop a List-

List<String> list = new ArrayList<String>();
        list.add("India");
        list.add("USA");
        list.add("UK");
        list.add("China");
       
        for(String s : list)
            System.out.println(s);

  // Output -  India, USA, UK ,China
* From  Java 8  Loop a ArrayList with forEach-

        List<String> list = new ArrayList<String>();
        list.add("India");
        list.add("USA");
        list.add("UK");
        list.add("China");
      
        // 1. Using lambda Expression
        // Output- India, USA, UK ,China
               list.forEach(item -> System.out.println(item));// iterate over for each item of list and print
      
        //  2. Name starts with "U"
        // Output- USA, UK
      
        list.forEach(item -> {                 // iterate over for each item of list
            if(item.startsWith("U"))        // check condition item start with "U" or not
            System.out.println(item);});    // print Item
      
      
        // 3. Using Method Refference
        // Output- India, USA, UK , China
      
        list.forEach(System.out::println);
      
        // 4. using  Stream and filter
        // Output - China , India
        list.stream()                                      // create new stream
        .filter(item -> item.contains("I"))    // Filter the results based on condition,
        .forEach(item -> System.out.println(item));       // Iterate over the filtered result and print. This is a terminal operation, Stream can not reuse after forEach.
                                                                                 
       
(II) forEach Loop and Map -  

 * Before Java 8 normal way to loop a Map -

Map<Integer, String > map = new HashMap<>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        map.put(4, "D");
      
        for(Map.Entry<Integer, String> me : map.entrySet())
            System.out.println("key is     "+ me.getKey() +"      value is     "+me.getValue());
        //Output -
        //key is     1      value is     A
        //key is     2      value is     B
        //key is     3      value is     C
        //key is     4      value is     D
       
* From java8 loop a map with forEach -

Map<Integer, String > map = new HashMap<>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        map.put(4, "D");

        // 1. Using forEach expression and lambda
        map.forEach((key,value)->System.out.println("Key is   "+key+"  value is  "+value));   // iterate over map for every key value pair
        //Output -
        //key is     1      value is     A
        //key is     2      value is     B
        //key is     3      value is     C
        //key is     4      value is     D

       
        //2 .checking condition using lambda
        map.forEach((key,value) -> { // Iterate over each key value pair
        if(key.equals(2))                   //Checking for Condition
            System.out.println("Welcome 2");}); // Print if condition satisfied

         //Output -
        //key is     2      value is     B
        


Comments