News

Q&A: Java 8 Lambdas and the Streaming APIs

App Dev Trends/Live! 360 speaker Michael Remijan shares how these will revolutionize the Java collection process, shows how it will change your code and talks about common misconceptions.

Java 8's lambdas and streaming APIs are the most major language update to Java since Java 5 introduced generics.  We talked with Michael Remijan, a system architect at the Federal Reserve Bank St. Louis, about his upcoming App Dev Trends/Live! 360 session on this topic to find out more.

What is one thing that most people don't understand about Streaming APIs and Lambdas?
Since Lambdas play such a big part of the Streaming API, most people believe they are one in the same, but the are not. Lambdas -- or, more specifically, functional interfaces -- are useful outside of the Streaming API, and it's surprising just how useful they can be.  If you keep Lambdas in mind while developing and you create well defined functional interfaces, your application can very easily provide many implementations using lambdas without the need to create a lot of small classes.  This is very nice.

Can you briefly explain how Streaming APIs have revolutionized the Java collection process?
I see it revolutionizing the Java collection process in two ways. First, it has completely turned collection processing on its head. Before Java 8, the imperative style of getting an iterator and looping over a collection means all the collection processing is done external to the collection. After Java 8, the functional style of passing a lambda to a stream means the collection processing is done internally.

Second, streams have made intermediate collections and maps no longer necessary. Before Java 8, collection processing that was complex and involved multiple steps mean many temporary/throwaway collections and maps had to be created before reaching the final results. After Java 8, all these go away since streams can be filtered and mapped to different streams or collected and reprocessed all before producing a final result.

Can you provide a simple code example to show how this technology can be used together for more concise and easier to understand code?
Let's suppose you have a List<Person> object and you want to sort this list first by the cities (alphabetically) that each person lives in, and then, within each city, you want the people living in that city listed by last name in **reverse** alphabetical order 

This is a complex, multiple step process which can easily be done with the Streaming API and lambdas.

// Sorted by city (alphabetically) and last name (reverse  alphabetically
  List<Person> result =
  persons.stream()
  .sorted(
  Comparator
  .comparing(Person::getCity)
  .thenComparing(Person::getLastName, Comparator.reverseOrder())
  )
.collect(Collectors.toList());

Is there anything you've seen in Java 8 Lambdas/Streaming APIs that you want to see further improved?
You can never have enough people on Stackoverflow answering questions.  The Streaming API and Lambdas are very feature rich and studying it all is a challenge.  But improving the knowledge of what's possible by asking questions on Stackoverflow benefits everyone.

Find out more about Live! 360 2016 in Orlando here.

About the Author

Becky Nagel is the director of online media for 1105's Enterprise Computing Group and executive editor for several of its sites, including Redmondmag.com and ADTmag.com. Contact her via e-mail or follow her on twitter at @beckynagel.

Featured

comments powered by Disqus
Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.