Monday, November 25, 2013

Java 8 Tutorials, Resources, Books and Examples to learn Lambdas, Stream API and Functional Interfaces

While release of Java 8 is still a few month away, GA is scheduled is in early next year (March 2014), Java community is already buzzing with expectation of functional programming and lambda expression. Many developers, programmers and bloggers has already shared lots of  really good tutorial to learn lambda expressions, , probably the next big thing in Java world . While the term "lambda expression" may sound abstract and academic, Java 8 Lambdas can have a big impact on how you program every day. Java 8 is coming up with  Lambda expressions, Stream API, Functional interfaces and default methods, which is eagerly waited through out the world. As a professional Java developer, you will have to learn Java 8 one day, and it's better to do sooner than later. One thing is sure that It's definitely going to change how you use Collection framework classes e.g. ArrayList or HashMap  in your Java program, especially on high performance applications. Not surprisingly, I am also started learning Java 8 new features and I am grateful to all these peoples, who has shared wonderful tutorials on lambdas. These are the people who have downloaded nightly builds, work through specification and initial draft to create, easy to understand Java 8 tutorials and examples. Since I am personally following them, it make a lot of sense for me to share all these resources with you guys, my readers. Earlier, when I shared some differences between Java and Scala, I had mentioned about some cool features of Scala, which makes code concise, clear and more readable, Java 8 is bringing parity on that as well. So stay tuned and follow these Java 8 Lambdas, Stream API and functional interfaces tutorial to get a feel of, what is going to be biggest change in Java programming language since it's first launch; probably even bigger than Generics from Java 1.5.


What is Lambda expression in Java

In simplest terms, a lambda expression is a function—a bit of code—that you can pass to another method as an argument. Thus, you can cope with changing requirements by using a behavior, represented by a lambda, as a parameter. If you have done some JavaScript programming then you know that you can pass anonymous function to any function, till Java 1.7 it was not possible in Java, you can only pass primitive or object to your methods. One part of Java API, which is going under substantial change is the famous and probably most used Java Collection Framework, which is being reworked to use Stream API. Java 8's functional programming features, like lambdas and the new Stream API  will provide a cleaner way to iterate through collections. They are also going to help you write concise, maintainable code that scales easily and performs well on multicore architectures.


Java 8 Tutorials on Lambda Expression, Stream API and Functional Interface

As I said, I have started learning  Java 8 new features to get myself ready to adopt Java 8 as soon as possible. I have a gut feeling that industry will probably do the same and once Java 8 will be formally released, there will be hardly a new project going to Java 1.6 or 7. Much like what happened after Generics was introduced, now you won't see any Collection framework code without Generics. There are lots of good Java 8 tutorials already available online, and I am going to make full use of those. I have selected some of the best available Java 8 tutorials to start with. This list is by no means complete and I will keep adding content on it, as and when I found something interesting and progress in my learning.



1. Maurice Naftalin's Lambda FAQ    
Whenever I talk about lambda expression of Java 8, this is the first resource, which is come to my mind. I have visited his lambda FAQ site lot of times and it's just amazing. If you don't remember Maurice Naftalin is one of the author of one of the great book Java Collections and Generics. He has done an absolutely gem of work to jolt down lambda expression FAQ, before going anywhere else, I strongly recommend to visit this site and go through those fundamental questions on Lambda expression. Just to give you a sneak peek of what you are going to get,  here are some questions from his Lambda FAQ
  •     What is a lambda expression?
  •     Why are lambda expressions being added to Java?
  •     What is a functional interface?
  •     Are lambda expressions objects?
  •     Where can lambda expressions be used?
  •     What are the scoping rules for lambda expressions?
  •     What is the type of a lambda expression?
  •     Can lambda expressions be used to define recursive functions?
  •     Can lambda expressions use variables from their environment?
  •     What are method references?
  •     What is the syntax for instance method references?
  •     What are constructor references?
I think Maurice is also working on new edition of Java Collections and Generics to include Java 8 changes, which will be truly great resource to start with. Eagerly waiting for that edition.


2) Official Lambda Expressions Tutorial from Oracle
This is another great tutorials on lambda expression. Like all other official Java tutorial, this one is also up to mark. It starts with a naive approach to use traditional Java coding style and it improves upon this approach with local and anonymous classes, and then finishes with an efficient and concise approach using lambda expressions. It also explains what is method references and how to use them. Since you use lambda expressions to create anonymous methods, remember not anonymous class. Sometimes, however, a lambda expression does nothing but call an existing method.  In those cases, it's often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name. For all starters, once you finished reading Maurice's lambda expression FAQ, you can start with this one. You will learn by doing comparative analysis opportunity presented by examples. For example, to print contents of a List, you can simply write  List.forEach(System.out::println), :: is used to denote method reference.

 
3) Java 8 Lambda Expression Presentations and Video tutorial
There are lots of presentation on Java 8 new features, lambda expressions, stream API, functional interfaces, default methods, how lambda expression works, pros and cons of using lambda expression and whole range of Java 8 topics. I have collected some of the best one which is delivered by authority like Brian Goetz, Java Language Architect at Oracle, and main author of  concurrency classic, Java Concurrency in Practice. His presentation, Lambda: A Peek Under the Hood  gives a wealth of technical detail about the implementation of lambda expression in Java. The Road to Lambda is another presentation by Brian Goetz, which provides a deep and comprehensive view of Project Lambda. Another good presentation is from JavaZone, , September 2012: Lambdas in Java 8 By Angelika Langer, who talked about lambda expressions, functional types, extension methods, and method references


4) Adopt OpenJDK Lambda Tutorial
This is another set of exercises to teach use of Java 8 lambda syntax, and the new Streams API. It contains proper instruction for doing this exercise hosted on Github, and some handy tutorials to setting up IDEs like Eclipse and IntelliJ for Java 8 lambda expression on Ubuntu, Mac OS and Windows. It also contains some good exercise on internal vs external iteration (the forEach method), Filtering and Collecting, Mapping and Method References. For now, you need to run JDK 8 nightly build to runt these tutorials and examples. You can get some more information from Official Project Lambda Page.



5) Java 8 Lambda tutorial from Dreamsys Software
This is one more collection of simple Java 8 tutorials on lambda expression. I like there easy to follow example on day to day topics. They have Java 8 tutorials divided into following topic :

Introduction - The basics.
Comparator - Using a comparator.
Collections Filtering - Filtering a collection.
Functions as Parameters - Passing functions as parameters.
Listeners - Using lambdas as listeners.
Map/Reduce - Using map/reduce with java 8.
DRY - DRY (Don't Repeat Yourself).

I would say very handy tutorial for busy Java developers.


6) IDE Support for Java 8 Lambda Expression
All three major Java IDEs e.g. Eclipse, IntelliJ and Netbeans has some form of support for lambda expression, but they are mostly unstable nightly build, only good for development and learning. I believe none of them have released stable version with JDK 8 support yet. Eclipse publishes nightly builds with JDK 8 support here, you can follow this tutorial from Oracle, about trying out lambda expressions in the Eclipse IDE to get yourself going. A Netbeans Nightly Build has JDK8-Support as well and you can check out http://wiki.netbeans.org/JDK8 for more information on Netbeans support of Java 8.



7) Java 8 Lambdas in Action
Java 8 Lambdas in Action is probably going to be first book on Java 8 support of lambda expression. It's not yet available but first few chapters are written already. It's one of the clearly-written guide to Java 8 lambdas and functional programming in Java. It begins with a practical introduction to the structure and benefits of lambda expressions in real-world Java code. The book then introduces the Stream API and shows how it can make collections-related code radically easier to understand and maintain. Along the way, you'll discover new functional programming oriented design patterns with Java 8 for code reuse, code readability, exception handling, data manipulation, and concurrency. For developers also exploring other functional languages on the JVM, the book concludes with a quick survey of useful functional features in Scala and Clojure. Good thing is that - First chapter of Java 8 Lambdas in Action is FREE, and you can download to take a sneak peak of it.

That's all folks, It's more than enough resources to learn key features of Java 8, mainly lambda expression, Stream API and functional interfaces. Google is your best friend, so when you feel stuck, you can always do a quick search and I am sure you will find something, which solves your problem, that's the biggest advantage of being part of this wonderful Java community.

Good luck and Enjoy learning Java 8 lambda expression.


Few more Books to learn Java 8 Programming

All Java 8 Books are only available for pre order and they are scheduled along with GA of Java 8, but good to see resources coming in thick and fast, which is another sign of Java 8's growing clout and popularity.

Java SE 8 for the Really Impatient by Cay Horstmann (Jan 27, 2014)
Java 8 Tutorial, Example and Resources to learn Lambda expression, Stream API


Java 8 Lambdas: Pragmatic Functional Programming by Richard Warburton (Apr 22, 2014)
Java 8 Lambda Books



Functional Programming in Java: Harnessing the Power Of Java 8 Lambda Expressions by Venkat Subramaniam (Feb 22, 2014)
Java 8 programming books and tutorials



6 comments :

Anonymous said...

Waiting for Java 8...!!!!

Manoj Das said...

Sir,

How did you setup ur eclipse for java 8?

ashok said...

Hi Manoj,

There is a patch for Eclipse Kepler which can be used for Java 8 or a latest version of Eclipse Luna is going to be released soon to support Java 8

Anuj Kumar said...

Why not use Nebeans Ide for Java 8..

Anonymous said...

I agree with Anuj, Netbeans has got best Java 8 support in big three IDEs. Infact, if you want to learn Java 8 quickly, use netbeans. It's content assist will suggest you to use lambda expression every time you cretae an annonymous class. It will also help you to understand when you can pass lambda expression to a method and when you cannot. Use of Runnable and Comparable will automatically use lambda expression in Netbeans. I have used it for just couple of days but I am already impressed with its Java 8 content assist.

Anonymous said...

Is any one is using IntelliJ IDEA for coding Java 8? how is their support with lambda expressions, streams and new Java 8 features?

Post a Comment