Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Java 8 Revealed: Lambdas, Default Methods and Bulk Data Operations

Now, I’ve been writing about this in bits and pieces for a few months now, and wanted to pull it all together into a single publication (you can download our pretty PDF version as well). I’m talking about Java 8, the long-awaited release. And even though some delays have occurred along the way, many believe it’s going to be worth it because of three major additions to the language: Lambdas, default (or defender) methods, and bulk data operations.

In case you’re hearing about it for the first time, Project Lambda is the major theme in the upcoming Java 8 and probably the most awaited feature for Java developers. Lambdas, as a language feature, do not come alone: to get most out of lambdas, you’re gonna need interface improvements for the standard JDK libraries.

The main driver behind the new lambda feature et al is the hardware trend of going towards multi-core. Chip designers have nowhere to go but parallel, and software developers must find out a better way to utilize the features of the underlying hardware. For application developers, and in our case Java developers, need simple parallel libraries to make their work more efficient. The obvious place to start for such an evolution is the parallelization of collections. Lambdas, it turns out, are great for improving the readability of your code and the expressiveness of the language.

It would be fair to say that without more language support for parallel idioms, people will instinctively reach for serial idioms. Hence, the new parallel idioms should be simple, intuitive and easy to use.

The code that we write today is inherently serial. Consider the following example:

List persons = asList(new Person("Joe"), new Person("Jim"), new Person("John"));
for(Person person : persons) {
  doSomething(person);
}

Historically, Java collections were not capable of expressing internal iterations, as the only way to describe an iteration flow was the for (or while) loop. The example above describes an iteration over the collection of elements and there’s no good way to express that the elements of the collection could be processed concurrently. However, with the new additions to the Java language, we could describe the same operation in a slightly different way:

List persons = asList(new Person("Joe"), new Person("Jim"), new Person("John"));
persons.forEach(this::doSomething);

Now, if the underlying library implementation actually supports parallel operations, the elements of the collection could be processed concurrently. We can just pass the operation into the forEach method and rely on it to perform computation for us. All the new language features that are being added to Java 8 are actually the tools to enable the path to multi-core and parallel libraries.

In this investigation, we provide the overview of the new language features in Java 8, such as lambda expressions and default methods. And as the goal of these language changes is to improve collections library, we will also cover some of new stuff in that area.

Here’s what you will learn about by reading this RebelLabs report:

  • Lambdas in Java 8: lambda syntax, SAM types, functional interfaces
  • Default methods: the what, why, and how
  • Bulk data operations for Java collections: the new way to express operations for data processing


Download the PDF

  • David

    should not it be persons.foreach(… ?

  • arhan

    Fixed. thx!

  • Martin

    Great post, thank you sir!

  • Vadim

    “Not that we can actually amend the lambda parameter type declaration:” Should it be ‘omit’ instead of ‘amend’?

  • arhan

    it should. thx.

  • Adam Boesch

    List persons = …
    Stream personsOver18 = persons.stream().filter(p -> p.getAge() > 18);

    This may seem obvious, but doesn’t the List need to be typed? Like for example:

    List persons = …
    Stream personsOver18 = persons.stream().filter(p -> p.getAge() > 18);

    Or are you just omitting it for readability reasons?

  • arhan

    Thanks for the question.

    It is just that I shamelessly forgot to add the generic type into the example, but let’s assume that is for the readability reasons :)

  • Martijn Blankestijn

    In the PDF (page 19) the last part (with ‘.map(Student::new);’ is missing on page 19

  • Stefan

    “Lambdas, it turns out, are great for improving the readability of your code and the expressiveness of the language.”

    This is one argument that I have seen on many places, but I don’t understand it. Does less information mean better readability? Or is it the amount of characters that you “have to” type?

    If less code/information means that it is more readable, then the following should be even more readable:

    cmp -> (x y) ? 1 : 0);

    I mean, it’s obvious that you are comparing x and y, so why would you “have to” declare them as parameters? The order could be determined by the order of appearance in the expression. Further on, what difference does it make if cmp is of Comparator type or not as long as you are providing an object with only one function that returns a value of desired type?

    I am sure that if I went on I could cut down the complete expression to something like:

    cmp(x,y) . ? 1 : 0;

    Very little code without any “unnecessary” information, but then you would have to spend a half an hour at every line of code trying to figure out what it does.

  • Jan Friderici

    Concise code (or normal English) is readable. Concise does not mean you should (or can) omit important information. It means you can (and should) omit redundant information.

  • Stefan

    Well, binary code is concise and doesn’t omit any important information, but it sure as hell isn’t readable. In written language, you have punctuation, and text formatting in documents in order to make it more readable. None of that is important information, but it improves readability.

  • Marco

    Removing boilerplate makes code more readable. Patching Java language to use lambdas does not make it more readable in it self, but the removal of thread control and creation anonymous classes by hand does make it more readable and useful. In the end it was done to make Java more relevant against other JVM languages.

  • Jan Friderici

    Binary code is everything but concise. A simple expression (in C, C++ or Java) can result in many single instructions.

    Punctuation can change the meaning of a sentence. eg “Stop clubbing baby seals” vs “Stop clubbing, baby seals”.

    Concise means to express much in few words, the exact opposite of binary code. Punctuation can be used to write concise or wordy sentences.

  • master faster

    where is the pdf version?

  • arhan

    There’s a button, “Dude, get the pdf” – push that.

  • Matthew McClellan

    The floating icons on the left are really annoying when your trying to read this site on mobile

  • http://www.brunokim.com.br/ Bruno Kim Medeiros Cesar

    Thanks for this post, it made me finally test Java 8.

    There’s a small error: IntStream.range does not accept an increment argument, so your example in Page 18 (at the PDF) doesn’t compile. Although I would mostly consider it an error from the implementor side…

  • arhan

    The API is evolving atm. At the time of writing I might have used a version where IntStream.range accepted the increment. I’m considering updating the doc when Java 8 is out.