foreach iteration in Lambda expressions- Functional Programming in JAVA 8- Part 8

Important feature in JAVA 8 specification is the way in which you loop over collections. There are different ways in which you can loop over collections, like JAVA 7 way, but in JAVA 8 way there is a whole new concept called Streams to iterate over collections. This concept combining with lambda expressions gives a whole lot of advantages.

Let’s take a look at below example where we have a list of People objects and we will be iteration over the list using for loop, foreach loop(for in actually), and foreach with lambda expression.

public class CollectionIterationExample {

	public static void main(String[] args) {
		List<Person> people=Arrays.asList(
				new Person("Charles","Dickens",60),
				new Person("Lewis","Carroll",42),
				new Person("Thomas","Carlyle",51),
				new Person("Charlotte","Bronte",45),
				new Person("Matthew","Arnold",39));

                System.out.println("Using for loop");
                for(int i=0; i<people.size(); i++){
                     System.out.println(people.get(i));
                }

                System.out.println("Using foreach loop");
                for(Person p: people){
                     System.out.println(p);
                }
       }
}

Both of the above iterators are very popular. Both of the above loops have something in common, they are external iterators, as you are writing code to perform iteration, you are controlling the iteration. You are deciding as where to start, where to end, what to do, and how to increment.

Let’s look at another example of iteration which is internal iterators, that’s JAVA 8 way, i.e you are giving control in internal JAVA runtime. You are just providing over what to iterate, that’s it. How the iteration happens is upto runtime.

Every collection in JAVA 8 has a method forEach(), it takes in a lambda expression of type Consumer as an argument.
You are basically telling the interpreter to execute the lambda expression for each item in collection.

public class CollectionIterationExample {

	public static void main(String[] args) {
		List<Person> people=Arrays.asList(
				new Person("Charles","Dickens",60),
				new Person("Lewis","Carroll",42),
				new Person("Thomas","Carlyle",51),
				new Person("Charlotte","Bronte",45),
				new Person("Matthew","Arnold",39));

                System.out.println("Using JAVA8 foreach loop");
                people.forEach(p->System.out.println(p));
       }
}

As you can see above there is no instruction of start and end.
Advantage of internal iterator over external iterators is, it makes easy for processor to run in multiple threads.

Below is sequential.

                for(int i=0; i<people.size(); i++){
                     System.out.println(people.get(i));
                }

This is also sequential.

                for(Person p: people){
                     System.out.println(p);
                }

No care about order, just run for each and every element.

people.forEach(p->System.out.println(p));

This makes it possible to run in parallel. Say you have 2 processors cores, runtime might say it gonna execute first 3 elements in the first core & last 2 elements in the second core.
This make us have parallelism.

Leave a Reply

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