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.

4 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes