RebelLabs-Java-8-cheat-sheet

 

Introduction

Parallelism in the software industry has become pretty common place since it’s the best way to achieve speed. The release of Java 8 has added some extremely beneficial features to the language. While those are not the main topic of this post, they are important for the concept of Java Streams and Java Parallel Streams. Just to add some scope, lets take a look at some of those other features.

Default methods

Anyone with experience writing in Java knows that multiple inheritance cannot be performed in Java (C++ on the other hand supports multiple inheritance). It is a true statement that multiple inheritance can be badly abused, but I firmly believe there are some cases where it makes sense. Java 8 provides the ability to add default implementations of methods on Java interfaces. This does not add multiple inheritance to the language, however it does allow for a pattern that developers can use to imitate multiple inheritance.

First Class Methods

Java 8 introduces the notion of methods as “first class citizens”. So what does this mean? This means that methods can be passed as parameters (similar to Javascript and other functional languages). This will be an important feature for the Java Streams API.

Lambda Expressions

Lambda expressions are another important feature of Java 8. They provide a similar functionality as First Class Methods. Lambda expressions can be thought of as anonymous functions, and viewed as purely functional. What does this mean? This means that they have some input and they should always produce output relative to their inputs. Think of a method that computes averages. You might pass that method a list of values to compute an average on. The internal workings of that method do not change, but the inputs and output do. Functional programming can also be a methodology. You don’t need to write lambdas to write functional code. Writing code in a functional manor can lead to ease when testing. It’s much easier to test code that is functional because in a test you want to have control over the inputs and outputs.

Streams

The Java Streams API uses many of the features described above to execute it’s goals. Streams provides a much cleaner interface for working with Java Collections. Imagine that you want to operate over a List of data. You might write an enhanced for loop to operate on the data, however their is a more concise way to do this in Java 8. Java 8 Streams provide some really great default functionality. The ability to filter a collection is as simple as providing a method or lambda so filter on.


roster
.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.forEach(e -> System.out.println(e.getName()));

The Streams API also provides the ability to perform reductions out of the box.


Integer totalAgeReduce = roster
.stream()
.map(Person::getAge)
.reduce(
0,
(a, b) -> a + b);

Parallel Streams

You may have noticed a pattern in the content above. It should not be a surprise that the new features above were released together. They all work in union to enhance Java and bring elements of functional languages to Java. I know personally I am really excited about all of them, but especially the notion of “Methods as first class citizens”… Ran into the multiple inheritance issue quite a few time!

This last topic build on top of all of the above. Scalability, contention, synchronization have all been issues to deal with when working with parallelism. Java 8 Parallel Streams attempts to simplify these issues by abstracting them. The concept seems note worthy, but it is important to look at all sides of the rubix cube. I point out the niceties of Parallel Streams, but there are also many posts out there describing real issues with abstracting parallelism. It’s easy to imagine that abstracting parallelism leads to a lack of control… The point is: use the right tool for the job. Check out this post describing what I mentioned above:

https://dzone.com/articles/think-twice-using-java-8

Here is an example of parallel streams in action:

double average = roster
.parallelStream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble();

Sources:

http://zeroturnaround.com/wp-content/uploads/2015/12/RebelLabs-Java-8-cheat-sheet.png

https://docs.oracle.com/javase/tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>