Sunday, December 8, 2013

Enhanced Collections API in Java 8- Supports Lambda expressions

Continuing with our exploration of JSR-335 lets look at some of the enhancements to the collections API as part of the Project Lambda effort. A new feature in the language and not supported by the existing API is just not what the programmers would want. And this is what Brian Goetz and his team working on JSR-335 realized. It was not an easy task of enhancing the existing API without breaking uncountable lines of code. That was a challenge which they took up and managed to get around this by introducing the concept of defender methods.
To outline the major changes in collection API:
  • Support for internal iteration by providing methods like forEach, filter, map and others which are added to the Iterable interface with default implementations (Defender methods).
  • Explicit parallel APIs for greater parallelism support. These can be combined with Fork/Join to divide the tasks
  • Greater stress on immutability and avoiding in-place mutation which was done in the conventional for-each loops
All of these are possible by giving the API/method power to decide its iteration. And the caller would pass in a block of code which would be applied on each of the element or elements decided by the API/method being invoked. That’s pretty much of the theory, lets see it in action on a List of integers from 1 to 10.
1List<Integer> counts = new ArrayList<Integer>();
2for(int i=1;i <= 10; i++){
3  counts.add(i);
4}
Iterating through the elements in the list:
1//Using external iterators
2System.out.println("Using external iterator");
3for(Integer i : counts){
4  System.out.print(i+" ");
5}
6System.out.println();
versus
1//Using internal iterators
2System.out.println("Using internal iterator");
3//Passing a code block to forEach method
4counts.forEach(i -> {System.out.print(i*2+" ");});
5System.out.println();
Very concise right? Digging into forEach method introduced in Iterable interface, it is implemented as:
1void forEach(Block<? super T> block) default {
2  Iterables.forEach(this, block);
3}
if you are confused about the use of “default” keyword, then please read about it here. Iterables is a new class to be introduced in Java 8 and the forEach method in Iterables expects an collection/something which can be iterated and the block of code to apply on the each element iterated, the implementation is:
1public static <T> Iterable<T>
2   forEach(final Iterable<? extends T> iterable,
3           final Block<? super T> block) {
4  Objects.requireNonNull(iterable);
5  Objects.requireNonNull(block);
6  for (T each : iterable) {
7    block.apply(each);
8  }
9 
10  return (Iterable<T>) iterable;
11}
Looks like forEach method does nothing different from the older for-each loop, that’s how forEach ought to work. If you want to maintain immutability you can use a different method which I will write a little later. But for now, forEach iterates through the iterable and applies the block to each of the element. Going into the Block and apply method would be out of scope of this article, lets look at it in a different article.
Lets explore a bit further and print all the even elements of the list.
Using external iteration ( for-each loop) we would have
1for ( Integer i : counts){
2  if ( i % 2 == 0 ){
3    System.out.print(i+" ");
4  }
5}
and using the new APIs:
1//filter evaluates the block
2//and returns a new iterable.
3counts.filter(i -> i%2 == 0)
4      .forEach(i-> {System.out.print(i+" ");});
the filter method doesn’t create a new collection instead it creates a new Iterable. The difference it makes is that there is no intermediate collection created after filter is invoked.
Another example before we close- lets multiply all the odd elements by 2 and create a new list.
Using the for-each loop, this can be done as:
1List<Integer> newCounts = new ArrayList<Integer>();
2for(Integer i : counts){
3  if ( i % 2 != 0 ){
4      newCounts.add(i*2);
5  }
6}
7for(Integer i : newCounts){
8System.out.print(i+" ");
9}
and the same using new API:
1List<Integer> newCountsNew = new ArrayList<Integer>();
2counts.filter(i -> i % 2 != 0)
3      .map(i -> i*2)
4      .into(newCountsNew);
5newCountsNew.forEach(i -> {System.out.print(i+" ");});
Apart from the other benefits mentioned at the beginning the above code which uses new APIs is more readable than the one which uses for-each.
If you want to read more about the state of the APIs with respect to the lambda expressions, please do so here.
The examples used above can be found here as well.

No comments: