Java 8 Tutorial:
Lambda Expressions, Streams, and More

Interested in live training from the author of these tutorials? See the upcoming Java 8 and general Java programming courses in Maryland, co-sponsored by Johns Hopkins Engineering for Professionals. Or, contact hall@coreservlets.com for info on customized Java, J2EE, Hadoop, Ajax/jQuery, and Android courses onsite at your location.


Following is a series of tutorials on the key new features of Java 8. Since each section includes exercises and exercise solutions, this can also be viewed as a self-paced Java 8 training course. These tutorials assume that you already know Java 5 or later; they focus on Java 8 topics that would not be understandable to those without at least moderate knowledge of interfaces, inner classes, and collections. If you don't already know the Java language, please see the Java programming tutorial series. All the slides, source code, exercises, and exercise solutions are free for unrestricted use.

Overview of the Java 8 Tutorial Series

Although lambda expressions are not quite real functions, they are close enough for most purposes, and have very succinct syntax that looks like Scala anonymous functions (minus the type declarations). Although it hardly makes Java into Lisp, lambdas support a definite functional programming style that is long overdue in Java.

Streams are wrappers around collections that use lambdas pervasively. They support many operations that use lambdas, including "map", "reduce", "filter", and "forEach". They also support lazy evaluation, so if you map firstName over Employees, filter ones that start with "P", then choose the first, it really only maps and filters until the first match. Streams can also be parallel, so that the operations are automatically done concurrently, without any explicit multithreading code.

Taken together, lambdas and streams result in by far the biggest change to Java programming since at least 2004, when generics and annotations were added to the language.

These tutorials are derived from Marty Hall's world-renowned live training courses. Customized courses on Java 8 or general Java programming are usually taught on-site at customer locations, but JSF 2, PrimeFaces, Spring, Hibernate, RESTful Web Services, Android, Hadoop, Ajax/jQuery, GWT, Java 7,and Java 8 training courses at public venues are periodically scheduled for people with too few developers for an onsite course. For descriptions of the various other courses that are available, please see the Java EE and Ajax training course page. To inquire about a customized training course at your location, please contact Marty at hall@coreservlets.com.

If you find these free tutorials helpful, we would appreciate it if you would link to us. Send corrections or feedback on any tutorial to hall@coreservlets.com.

Java 8: Installation, Setup, and Review of Foundational Topics

Java 8 Lambda Expressions Part 1

Java 8 Lambda Expressions Part 2

Java 8 Lambda Expressions Part 3

  • Tutorial section in PDF (best for printing and saving).
  • Tutorial section on SlideShare (preferred by some for online viewing).
  • Topics covered:
    • Variable scoping: Local variables, instance variables, "this"
    • Method references: details -- Class::staticMethod, variable::instanceMethod Class::instanceMethod, Class::new
    • New features in Java 8 interfaces: Default methods (also called "virtual extension methods" or "defender methods") and static methods
    • Methods that return lambdas: From Predicate: and, or, negate, isEqual; From Function: andThen, compose, identity; From Consumer: chain; Custom methods
  • Source code of examples for the entire Java 8 tutorial. Bundled as a NetBeans project, but could be easily copied into an Eclipse project once Eclipse supports Java 8.

Java 8 Streams Part 1

Java 8 Streams Part 2

Customized Onsite Training

Looking for short hands-on training courses on Java 8, general Java programming, or other Java-related topics taught at your company? The courses are taught by Marty Hall, an experienced instructor, developer, speaker, and well-known Java EE author, and the creator of this tutorial series. For more information, see the JSF and PrimeFaces training course page or contact Marty to arrange a course at your location. Courses are available in any country, and have been given in N. America, Central America, Europe, Asia, and Australia. Distinctive features of Marty's training:


  • Experienced instructor who can answer tough questions and show best practices.
    Marty Hall and James Gosling @ Marty's car

    Marty & Java inventor James Gosling at Marty's car.

    With the big training vendors, you don't even know the instructor's name before the class begins, much less their credentials. Marty is an experienced real-world developer, award-winning instructor, conference speaker on servlets and JSP (including five times at JavaOne), and author of six popular J2EE books from Prentice Hall and Sun Microsystems Press. Core Servlets & JavaServer Pages is the most popular servlet and JSP book internationally, with translations in Bulgarian, Chinese (both traditional and simplified script), Czech, French, German, Hebrew, Japanese, Korean, Macedonian, Polish, Portuguese, Russian and Spanish. Marty also teaches in the Johns Hopkins University part-time graduate program in Computer Science, where he directs the Java and Web-related concentration area. Click here for more details.
  • Well-tested courses. Marty has taught J2EE training courses in Canada, Australia, Japan, Puerto Rico, Mexico, India, Cambodia, Norway, the Philippines, and dozens of US venues. Clients for on-site Java courses include Symantec, VeriSign, the NY Stock Exchange (SIAC), State Farm Insurance, General Motors, Hewlett Packard, Lexmark International, TransCanada Pipelines, Raytheon, Learjet, Sikorsky, Los Alamos National Labs, Genomica, the U.S. Navy, General Dynamics, Banco Popular, Titan Corporation, the University of Michigan, Context Integration, the University of Utah Hospital, ESI, SAIC, NSA, CIA, and many others. Click here for a longer list. No using your developers as guinea pigs!
  • Flexible schedules. Courses are available in any city worldwide, and have been taught in dozens of US venues and 9 other countries. However, Maryland-area companies have extra flexibility in the course schedules, since Marty is local to Maryland. In addition to the full-day courses offered in all locations, Maryland companies can book the courses in the late afternoons or early evenings. Even weekends are possible! For example, Marty has done about a dozen Maryland courses that met once or twice a week from 4:30-6:30 pm (or similar) so that the students did not lose billable hours.
  • Course materials developed by the instructor. Marty personally developed all of his own materials, much of it based on his best-selling books. They thoroughly cover the basics, introduce advanced topics, include many meaty code examples, and show plenty of live running applications. They also discuss best practices, design strategies, and efficiency issues. Most of the big training vendors hire someone to create the course materials, then bring in some inexperienced flunky to regurgitate them to the class.
  • Public (open-enrollment) training courses
    JHU/EP Logo Onsite courses are cheaper and more convenient for organizations that have medium to large sized groups of developers. But, in order to serve organizations with only one or two developers that need training, coreservlets.com periodically offers public versions of some of the most popular courses. The next public offerings will be on Java 7 programming, new features in Java 8, Android programming, Web application development with JSF2, PrimeFaces, Big-Data Apps with Hadoop, Rich Internet Applications with Ajax (and jQuery), GWT programming, Hadoop, Java EE programming with Spring, Java persistence with Hibernate and JPA, and Web Services (RESTful and SOAP-based). The 2013 courses are co-sponsored by Johns Hopkins Engineering for Professionals and will be held at JHU/Dorsey in Elkridge Maryland (5 minutes from BWI airport). Click here for details.
  • A variety of complementary course offerings.
    Promos for Marty Hall at GIDS conference in Bangalore

    Ads for Marty at GIDS conference in India

    Marty personally teaches the following courses (click for detailed course descriptions): In addition, Marty reviews/edits courses taught by colleagues. These courses are developed by experts who work daily with the technologies, and the materials are thoroughly reviewed by Marty. Course topics include:
  • Customizable content. Want a class on JSF 2 or Spring or Hadoop, but need some review of basic Java first? No problem. Want to move extra fast because your developers are experienced? Piece of cake. Need to combine some basic JSF topics with thorough coverage of PrimeFaces? Just say the word. Want an Ajax and jQuery course, but first need a crash course on JavaScript syntax, first-class functions, and the DOM? Simply let us know. Want training on GWT, but need a review of basic server-side Java techniques first? Tell us what you need. Every onsite course is fully customized to the needs and backgrounds of your developers.
  • Competitive prices. Marty's public training courses are typically at least 10% cheaper than the canned courses from the big training vendors. And his courses include a textbook, a bound student notebook, exercises, exercise solutions, and all necessary software (for onsite courses). Onsite courses at your location are even cheaper, since there are no venue rental costs. Plus, your developers don't have to pay travel costs.

Email for details.

PowerPoint Files for University Faculty

The PDF files in this tutorial contain the complete text of the original PowerPoint files, so if your goal is learning this technology, just stick with this tutorial. However, as a service to instructors teaching full-semester courses at accredited universities, coreservlets.com will release the original PowerPoint files for free. Please see the instructor materials page for details.

More Information

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts