Java 8′s Functional Fomentation

revolution_smallJava 8 has revolutionized Java. It’s easily the most significant release of Java in the last 10 years. There are a ton of new features including default methods, method and constructor references, and lambdas, just to name a few.

One of the more interesting features is the new java.util.stream API, which as the Javadoc states, enables

 

functional-style operations on streams of elements, such as map-reduce transformations on collections

Combine this new API with lambda expressions and you end up with a terse, yet, powerful syntax that significantly simplifies code through the application of projections.

Take, for example, the ostensibly simple task of filtering a collection. In this case, a simple Collection of Message types, created like so:

Creating a Collection of Messages

List<Message> messages = new ArrayList<>();
messages.add(new Message("aglover", "foo", 56854));
messages.add(new Message("aglover", "foo", 85));
messages.add(new Message("aglover", "bar", 9999));
messages.add(new Message("rsmith", "foo", 4564));

With this collection, I’d like to filter out Messages with a delay (3rd constructor parameter) greater than 3,000 seconds.

Previous to Java 8, you could hand jam this sort of logic like so:

Filtering old school style

for (Message message : messages) {
  if (message.delay > 3000) {
    System.out.println(message);
  }
}

In Java 8, however, this job becomes a lot more concise. Collections now support the stream method, which converts the underlying data structure into a iterate-able steam of objects and thereby permits a new breed of functional operations that leverage lambda expressions. Most of these operations can be chained as well. These chain-able methods are dubbed intermediate, methods that cannot be chained are denoted as terminal.

Briefly, lambda expressions are a lot like anonymous classes except with a lot less syntax. For example, if you look at the Javadocs for the parameter to a Stream’s filter method, you’ll see that it takes a Predicate type. Yet, you don’t have to implement this interface as you would, say, before Java 8 with an anonymous class. Consequently, the Predicate lambda expression for filtering all values of delay greater than 3000 would be:

Lambda expression

x -> x.delay > 3000

Where x is the parameter passed in for each value in the stream and everything to the right of the -> being the expression evaluated.

Putting this all together in Java 8 yields:

Streaming lambdas!

messages.stream().filter(m -> m.delay > 3000).forEach(item -> System.out.println(item));

Interestingly, due to some other new features of Java 8, the forEach’s lambda can be simplified further to:

Streaming lambdas are even shorter!

messages.stream().filter(m -> m.delay > 3000).forEach(System.out::println);

Because the parameter of the forEach lambda is simply consumed by the println, Java 8 now permits you to drop the parameter entirely.

Earlier, I mentioned that streams permit you to chain lambdas – in the case above, the filter method is an intermediate method, while the forEach is a terminal method. Other intermediate methods, that are immediately recognizable to functional programmers, are: map, flatMap, and reduce, to name a few.

To elaborate, I’d like to find all Messages that are delayed more than 3,000 seconds and sum up the total delay time. Without functional magic, I could write:

Prosaic Java

long totalWaitTime = 0;
for (Message message : messages) {
  if (message.delay > 3000) {
    totalWaitTime += message.delay;
  }
}

Nevertheless, with Java 8 and a bit of functional-foo, you can achieve a more elegant code construct like so:

Java 8 elegance

long totWaitTime = messages.stream().filter(m -> m.delay > 3000).mapToLong(m -> m.delay).sum();

Note how I am able to chain the filter and mapToLong methods, along with a terminal sum. Incidentally, the sum method requires a specific map style method that yields a collection of primitive types, such as mapToLong, mapToInt, etc.

Functional style programming as a core language feature is an astoundingly powerful construct. And while a lot of these techniques have been available in various 3rd party libraries like Guava and JVM languages like Scala and Groovy, having these features core to the language will surely reach a wider audience of developers and have the biggest impact to the developmental landscape.

Java 8, without a doubt, drastically changes the Java language for the better.

Reference: Java 8′s Functional Fomentation from our JCG partner Andrew Glover at the The Disco Blog blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Java 8′s Functional Fomentation"

  1. crusader says:

    I understand Java 8 has launched new features, but hey who requires these complicate features
    I dont think anybody will use them

Leave a Reply


5 − one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books