Stream ForEach

The forEach is a Terminal Operation. forEach is defined in Iterable interface as abstract for all Collections classes except Map, in Map interface as default method for Map Classes, and default method in Stream Interface. it takes Consumer as an argument. forEach iterate over the Stream of elements, and Collections and perform an action over each element.

The forEach is not used to update the Collection, Map or Stream

The traditional for loop keeps tracks of indexes, which is not possible in forEach.

The forEachcannot iterate backwards, which is allowed in traditional for loop

The for loop can jump indexes, which is not possible in forEach, it will iterate every element of the Stream.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

class Apple {
    public static void main(String args[]) {
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        System.out.println("*---------------------------Traditional method---------------------------*");
        for (Integer integer : integers) {
            System.out.println(integer);
        }

        Consumer<Integer> print = System.out::println;

        System.out.println("*---------------------------Consumer---------------------------*");
        integers.stream().forEach(print);

        System.out.println("*---------------------------Consumer as method argument---------------------------*");
        integers.stream().forEach(integer -> print(integer, print));

        System.out.println("*---------------------------Inline Consumer---------------------------*");
        integers.stream().forEach(System.out::println);

        System.out.println("*---------------------------Lambda Expression---------------------------*");
        integers.stream().forEach(integer -> System.out.println(integer));
    }

    private static void print(Integer integer, Consumer<Integer> consumer) {
        consumer.accept(integer);
    }
}

ForEach on Map

The forEach is defined in the Map interface as the default method. It takes BiConsumer for key and value

package org.wesome.java8;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

class Apple {
    public static void main(String args[]) {
        Map<Integer, String> map = new HashMap();
        map.put(1, "Macintosh");
        map.put(2, "Fuji");
        map.put(3, "Gala");
        map.put(4, "Jonagold");

        System.out.println("*---------------------------Traditional method---------------------------*");
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

        Consumer<Map.Entry<Integer, String>> print = System.out::println;

        System.out.println("*---------------------------Consumer---------------------------*");
        map.entrySet().stream().forEach(print);

        System.out.println("*---------------------------Consumer as method argument---------------------------*");
        map.entrySet().stream().forEach(entry -> printMap(entry, print));

        System.out.println("*---------------------------Inline Consumer---------------------------*");
        map.entrySet().stream().forEach(System.out::println);

        System.out.println("*---------------------------Lambda Expression---------------------------*");
        map.entrySet().stream().forEach(entry -> System.out.println(entry));

        System.out.println("*---------------------------BiConsumer---------------------------*");
        map.forEach((key, value) -> System.out.println("key = " + key + " value = " + value));
    }

    private static void printMap(Map.Entry<Integer, String> map, Consumer consumer) {
        consumer.accept(map);
    }
}

ForEach on User Defined Object

package org.wesome.java8;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@Data
@AllArgsConstructor
class Fruit {
    int fruitId;
    String fruitName;
}

class Apple {
    public static void main(String args[]) {
        List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh"), new Fruit(2, "Fuji"), new Fruit(3, "Gala"), new Fruit(4, "Jonagold"));

        System.out.println("*---------------------------Traditional method---------------------------*");
        for (Fruit fruit : fruits) {
            System.out.println(fruit);
        }

        Consumer<Fruit> print = System.out::println;

        System.out.println("*---------------------------Consumer---------------------------*");
        fruits.stream().forEach(print);

        System.out.println("*---------------------------Consumer as method argument---------------------------*");
        fruits.stream().forEach(entry -> filteredList(entry, print));

        System.out.println("*---------------------------Inline Consumer---------------------------*");
        fruits.stream().forEach(System.out::println);

        System.out.println("*---------------------------Inline Consumer---------------------------*");
        fruits.stream().forEach(fruit -> System.out.println(fruit));
    }

    private static void filteredList(Fruit fruit, Consumer<Fruit> consumer) {
        consumer.accept(fruit);
    }
}

 

follow us on