Capa

Practical Java 8

Index

  • 1 - Java 8
    • 1.1 - Hold your horses!
    • 1.2 - Access the code and talk to us!
  • 2 - Hello Lambda!
    • 2.1 - The old way and the new way of doing loops
    • 2.2 - Let Lambda in!
  • 3 - Functional Interfaces
    • 3.1 - Another Example: listeners
    • 3.2 - Your very own functional interface
    • 3.3 - The @FunctionalInterface annotation
    • 3.4 - Digging deeper: the first details
  • 4 - Default Methods
    • 4.1 - The forEach method in the Iterable interface
    • 4.2 - The Consumer interface doesn’t have only one method!
    • 4.3 - Another new method in Collection: removelf
    • 4.4 - Multiple Inheritance?
  • 5 - Sorting in Java 8
    • 5.1 - Comparators using lambda
    • 5.2 - The List.sort method
    • 5.3 - Static methods in the Comparator Interface
    • 5.4 - Knowing the Comparator.comparing better
    • 5.5 - Sorting by score and the autoboxing
  • 6 - Method References
    • 6.1 - Turning all users into moderators
    • 6.2 - Comparing in an even lighter way
    • 6.3 - Composing comparators
    • 6.4 - Referencing instance methods
    • 6.5 - Referencing methods that receive arguments
    • 6.6 - Referencing constructors
    • 6.7 - Other types of references
  • 7 - Streams and Collectors
    • 7.1 - Making the top 10 users with highest scores moderators
    • 7.2 - Streams: making the users with a score higher than 100 moderators
    • 7.3 - How to get a List back?
    • 7.4 - Collectors
    • 7.5 - Advanced: why isn’t there a toList in Stream?
    • 7.6 - Listing only the scores of all users by using map
    • 7.7 - InStream and the Streams family
    • 7.8 - Optional in java.util
  • 8 - More operations with Streams
    • 8.1 - Sorting a Stream
    • 8.2 - Many operations in Stream are lazy!
    • 8.3 - what is the advantage of lazy methods?
    • 8.4 - Seeing at the pipeline executing with peek
    • 8.5 - Reduction operations
    • 8.6 - Learning about other Stream methods
    • 8.7 - Primitive and infinite streams
    • 8.8 - Practicing what we learn with java.nio.file.Files
    • 8.9 - FlatMap
  • 9 - Mapping, partitioning, grouping, and parallelizing
    • 9.1 - Collectors generating maps
    • 9.2 - groupingBy and partitioningBy
    • 9.3 - Running the pipeline in parallel
    • 9.4 - Nondeterministic operations and ordered streams
  • 10 - Enough with Calendar! New date API
    • 10.1 - Java.time comes from Joda Time
    • 10.2 - Working with dates fluently
    • 10.3 - Enums instead of constants
    • 10.4 - Formatting in the new date API
    • 10.5 - Invalid dates
    • 10.6 - Duration and Period
    • 10.7 - Differences from Joda Time
  • 11 - A payment model with Java 8
    • 11.1 - A digital goodies store
    • 11.2 - Reducing BigDecimal into sums
    • 11.3 - Best selling products
    • 11.4 - Value generated by product
    • 11.5 - What are the products of each customer?
    • 11.6 - Which one is our most special client?
    • 11.7 - Reports with dates
    • 11.8 - Subscription System
  • 12 - Appendix: more Java 8 with reflection, JVM, APIs, and limitations
    • 12.1 - New details in the language
    • 12.2 - What is the type of a Lambda expression?
    • 12.3 - Inference limitations in Lambda
    • 12.4 - End of PermGen
    • 12.5 - Reflection: parameter names
Chapter1

Java 8

It has been almost 20 years since the first version of Java was launched in 1996.

With the arrival of Java 5 in 2004, the programming language underwent significant changes -- especially with Generics, Enums, and Annotations.

With its 2014 incarnation, Java 8, the same scenario occurs. We now face new possibilities with the introduction of Lambda and Method References, in addition to other small changes. The Collections API, which includes the same main interfaces since 1998, went through a significant upgrade with the introduction of Streams and default methods.

In this book, you will have the chance to practice Java 8 extensively. It’s time to start programming, but first you will need to download and install the Java platform:

http://www.oracle.com/technetwork/java/javase/downloads/

You can access the Java 8 API documentation here:

http://docs.oracle.com/javase/8/docs/api/

Eclipse supports Java 8 since the Luna (4.4) version. You will need the following update for Kepler (4.3):

https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

Eclipse still has some minor bugs when it comes to performing more complicated inferences, but Netbeans and IntelliJ have updated their Java 8 versions.

To practice the syntax, you can choose to complete the tests and examples from the book with a simple text editor.

1.1 - Hold your horses!

If you expect something on the scale of Scala, Clojure, or C#, be prepared for disappointment. Java’s legacy and relatively young age, besides its lack of value types and reification of Generics, preclude the use of certain strategies. The Java Development Team takes great care to keep the syntax simple, as to avoid obscure features that would only bring little gain. (In our view, this makes perfect sense.)

On the other hand, it is amazing what has been achieved with the release of Java 8. You might be pleasantly surprised by some of the codes and approaches used. The focus is not breaking the compatibility of legacy Java code, while being the least intrusive with regards to the older APIs. Its new feature, the Streams, will play a crucial role in this elegant upgrade.

 

What was left out of Java 8?

 

In order to better break down Java 8’s specifications into smaller tasks, the JEPs (JDK Enhancement Proposals) have been created -- the inspiration came from the Python community’s PEPs. You can find those in a list of proposals here:

http://openjdk.java.net/jeps/0

As you can see, there are many new features in JDK8. Unfortunately, not all of these features had enough time to ripen. Among the JEPs, the Value Objects were left out:

http://openjdk.java.net/jeps/169

That was also the case for the use of literals when working with Collections:

http://openjdk.java.net/jeps/186

Among other ideas left out, there were several improvements to the already built-in Garbage Collectors, as well as the possible reification of Generics.

In any case, the absolute majority of the JEPs made it to the final version and were released. Throughout the book, we will see the major changes made to the language and the new APIs.

1.2 - Access the code and talk to us!

The source code for each chapter can be found here:

https://github.com/codeslashers/java8

We recommend that you alone write all the codes presented in this book so as to practice the API and syntax. We also advise you to perform different tests from those suggested.

Here is a forum where you will be able to discuss with us or send us your suggestions or comments:

https://groups.google.com/forum/#!forum/codeslashers-java8