Practical Java 8: Lambdas, Streams and new resources
By Paulo Silveira and Rodrigo Turini
5/5
()
About this ebook
Related to Practical Java 8
Related ebooks
Java Coding Problems: Improve your Java Programming skills by solving real-world coding challenges Rating: 0 out of 5 stars0 ratingsJava 9 Data Structures and Algorithms Rating: 0 out of 5 stars0 ratingsModern Java in Action: Lambdas, streams, functional and reactive programming Rating: 3 out of 5 stars3/5Learn Java 12 Programming: A step-by-step guide to learning essential concepts in Java SE 10, 11, and 12 Rating: 0 out of 5 stars0 ratingsJava: Tips and Tricks to Programming Code with Java Rating: 0 out of 5 stars0 ratingsGrokking the Java Interview Rating: 0 out of 5 stars0 ratingsJava Core Interview in Australia. Questions and Answers. Tech interviewer’s notes Rating: 0 out of 5 stars0 ratingsCore Java Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 4 out of 5 stars4/5OCA / OCP Java SE 8 Programmer Practice Tests Rating: 5 out of 5 stars5/5OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z0-809 Rating: 5 out of 5 stars5/5Java and Java EE Interview Preparations Rating: 0 out of 5 stars0 ratingsBrush-up java for Interview Rating: 5 out of 5 stars5/5Introduction to JVM Languages Rating: 0 out of 5 stars0 ratingsJava Core Interview Questions and Answers. Tech interviewer’s notes Rating: 1 out of 5 stars1/5Learning RxJava Rating: 4 out of 5 stars4/5Core Java Professional: For First Time Learner's. Rating: 0 out of 5 stars0 ratingsOCP Oracle Certified Professional Java SE 11 Programmer II Study Guide: Exam 1Z0-816 and Exam 1Z0-817 Rating: 5 out of 5 stars5/5Java 9 Programming By Example Rating: 4 out of 5 stars4/5OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 Rating: 5 out of 5 stars5/5Java: Best Practices to Programming Code with Java: Java Computer Programming, #3 Rating: 0 out of 5 stars0 ratingsJava: Advanced Guide to Programming Code with Java: Java Computer Programming, #4 Rating: 0 out of 5 stars0 ratingsJAVA for Beginner's Crash Course: Java for Beginners Guide to Program Java, jQuery, & Java Programming Rating: 4 out of 5 stars4/5Learn Java: A Crash Course Guide to Learn Java in 1 Week Rating: 3 out of 5 stars3/5Testing with JUnit Rating: 0 out of 5 stars0 ratingsEnterprise Java Microservices Rating: 0 out of 5 stars0 ratingsJava: Tips and Tricks to Programming Code with Java: Java Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsCrystal Clear Java: 1St Edition Rating: 0 out of 5 stars0 ratingsOCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide: Exam 1Z0-815, Exam 1Z0-816, and Exam 1Z0-817 Rating: 0 out of 5 stars0 ratingsJava 8 to 21: Explore and work with the cutting-edge features of Java 21 (English Edition) Rating: 0 out of 5 stars0 ratings
Programming For You
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Ethical Hacking Rating: 4 out of 5 stars4/5Think Like a Programmer: An Introduction to Creative Problem Solving Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsSpies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5
Reviews for Practical Java 8
1 rating1 review
- Rating: 5 out of 5 stars5/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