We all have extensively used Collection classes like List, Map and their derived versions. And each time we used them we had to iterate through them to either find some element or update the elements or find different elements matching some condition. Consider a List of Person as shown below:

List<Person> personList = new ArrayList<>();
personList.add(new Person("Virat", "Kohli",22));
personList.add(new Person("Arun", "Kumar",25));
personList.add(new Person("Rajesh", "Mohan", 32));
personList.add(new Person("Rahul", "Dravid", 35));

To find out all the Person instances with age greater than 30, we would do:

List<Person> olderThan30OldWay = new ArrayList<>();
for ( Person p : personList){
  if ( p.age >= 30){
    olderThan30OldWay.add(p);
  }
}
System.out.println(olderThan30OldWay);

and this gives me the output as:

[Rajesh Mohan, 32, Rahul Dravid, 35]

The code is easy to write, but is it not a bit more verbose, especially the iteration part? Why would we have to iterate? Would it not be cool if there was an API which would iterate the contents and give us the end result i.e we give the source List and use a series of method calls to get us the result List we are looking for? Yes, this is possible in other languages like Scala, Groovy which support passing closures and also support internal iteration. But is there a solution for Java developers? Yes, this exact problem is being solved by introducing support for Lambda Expressions(closures) and a enhanced Collection API to leverage the lambda expression support. The sad news is that it’s going to be part of Java 8 and will take some time to be into mainstream development.

Leveraging the Java 8 enhancements to the above scenario

As I said before the Collections API is being enhanced to support the use of Lambda Expression and more about it can be read here. Instead of adding all the new APIs to the Collection class the JDK team created a new concept called “Stream” and added most of the APIs in that class. “Stream” is a sequence of elements obtained from the Collection from which it is created. To read more about the origin of Stream class please refer to this document.

To implement the example I started with using the enhancements in Java 8 we would be using few new APIs namely: stream(), filter(), collect(), Collectors.toCollection().
stream(): Uses the collection on which this API is called to create an instance of Stream class.
filter():This method accepts a lambda expression which takes in one parameter and returns a boolean value. This lambda expression is written as a replacement for implementing the Predicate class.
collect(): There are 2 overloaded versions of this method. The one I am using here takes an instance of Collector. This method takes the contents of the stream and constructs another collection. This construction logic is defined by the Collector.
Collectors.toCollection(): Collectors is a factory for Collector. And the toCollection() takes a Lambda expression/Method reference which should return a new instance of any derivatives of Collection class.

With brief introduction to the APIs used, let me show the code which is equivalent to the first code sample:

List<Person> olderThan30 = 
    //Create a Stream from the personList
    personList.stream().
    //filter the element to select only those with age >= 30
    filter(p -> p.age >= 30).
    //put those filtered elements into a new List.
    collect(Collectors.toCollection(() -> new ArrayList<Person>()));
System.out.println(olderThan30);

The above code uses both Internal iteration and lambda expressions to make it intuitive, concise and soothing to the eye.
If you are not familiar with the idea of Lambda Expressions, check out my previous entry which covers in brief about Lambda expressions.

Tagged with:
 
About The Author

Mohamed Sanaulla

One Response to Extracting the elements of the Java Collection- The Java 8 way

  1. [...] I was working on the samples for this post using the Nebeans nightly builds with support for Lambda Expressions downloaded from here. I [...]

Leave a Reply