Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Practical Java 8: Lambdas, Streams and new resources
Practical Java 8: Lambdas, Streams and new resources
Practical Java 8: Lambdas, Streams and new resources
Ebook199 pages2 hours

Practical Java 8: Lambdas, Streams and new resources

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

Are you a programmer fluent in Java? It's time to take the next step! Almost 20 years after it's first version, there is a new Java with important news! Amongst the main new resources, there are the default methods, method references and lambdas. They are simple concepts, but bring important possibilities. In the book we will explore various advancements found in Java 8. Always using practical examples and presenting real usage cases, we migrate the day-to-day legacy code to the new Java 8 functional paradigm. With this triad of concepts, the API managed to evolve in an interesting manner. The java.util.stream and java.util.function packages are deeply explored, presenting simple ways to work with collections and other data structures. Streams and Collectors will be part of your routine and will become as essential as Collections and the java.io currently are to your applications. Lastly, we'll see how the new API java.time comes to change from water to wine the way how we work with dates and times.
LanguageEnglish
Release dateJan 1, 2014
ISBN9788566250558
Practical Java 8: Lambdas, Streams and new resources

Related to Practical Java 8

Related ebooks

Programming For You

View More

Reviews for Practical Java 8

Rating: 5 out of 5 stars
5/5

1 rating1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5

    Nov 27, 2023

    Excelent book about Java, overview in all of content in the Java 8 release.

Book preview

Practical Java 8 - Paulo Silveira

Sumário

Capítulo 1: Java 8

1.1 Hold your horses!

1.2 Access the code and talk to us!

Capítulo 2: Hello Lambda!

2.1 The old way and the new way of doing loops

2.2 Let Lambda in!

Capítulo 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

Capítulo 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?

Capítulo 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

Capítulo 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

Capítulo 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

Capítulo 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

Capítulo 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

Capítulo 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

Capítulo 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

Capítulo 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

Go to Code Crushing and see our other e-books - www.codecrushing.com.

Capítulo 1:

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

Capítulo 2:

Hello Lambda!

Instead of starting the course with theory, you should first get a feel of how Java 8 will change your way of programming.

2.1 The old way and the new way of doing loops

It is important that you follow the book and recreate the code presented. This way, the syntax will become more natural and familiar to you.

Open your favorite text editor. Let’s create an entity and run examples based on it. Doing so, we will have the class User with three basic attributes: reputationScore, name, and boolean becomeModerator, indicating that this user will be a moderator in our system, just like this:

1 class User { 2 3   private int reputationScore; 4   private String name; 5   private boolean becomeModerator; 6 7   public User (String name, int reputationScore) { 8     this.reputationScore = reputationScore; 9     this.name = name; 10     becomeModerator = false; 11   } 12 13   public int getReputationScore() { 14     return reputationScore; 15   } 16 17   public String getName(){ 18     return name; 19   } 20 21   public void becomeModerator(){ 22     becomeModerator = true; 23   } 24 25   public boolean isModerator(){ 26     return becomeModerator; 27   } 28 }

We decided not to make the class public. This way, if you are in a simple text editor, you can create your tests in a public class within the same file.

Let’s handle a few users with their different names and scores and print each one of them out. We will do this the usual way, the way we already know, without using any of Java 8’s new features.

1 public class Chapter2 { 2   3   public static void main(String... args) { 4     5     User user1 = new User(Paulo Silveira, 150); 6     User user2 = new User(Rodrigo Turini, 120); 7     User user3 = new User(Guilherme Silveira, 190); 8 9     List users = Arrays.asList(user1, user2, user3); 10 11

Enjoying the preview?
Page 1 of 1