A lifetime effort! 20 years of experience as a Java architect makes this Java practical note

Preface

Simply put, the new features in Java 8 and the changes introduced by Java 9 (though not significant) are the biggest changes in Java since Java 1.0 was released 21 years ago. This evolution does not remove anything, so your original Java code can work, but the new features provide more powerful new idioms and new design patterns that can help you write clearer and more concise code. Just like when encountering all new features, you might think at first: "Why do you want to change my language again?" But after a little practice, you will find that you only use the new one in half of the expected time. The function has written shorter and clearer code, and then you will realize that you will never be able to return to the "old Java".

This article will help you cross the threshold of "the principle sounds good, but it's still a bit new and not very adaptable", so that you can program proficiently.

"Maybe," you might think, "but Lambda, functional programming, aren't these things that beards and sandals scholastics are thinking about in the ivory tower?" Maybe yes, but it was added in Java 8. The weight of new ideas is just right, and the benefits they bring can be understood by ordinary Java programmers.

This article will narrate from the perspective of ordinary programmers, occasionally talk about "how did this come".

"Lambda, it sounds like the heavenly book!" Yes, maybe it is, but it is a good idea that allows you to write concise Java programs. Many people are familiar with event handlers and callback functions, that is, registering an object, which contains a method that will be used when an event occurs.

Lambda makes it easier to apply this idea widely in Java. Simply put, Lambda and its friends "method references" allow you to concisely pass code or methods as parameters for execution while doing other things.

In this article, you will see this idea appear more frequently than expected: from adding code for comparison to simply parameterize a sorting method, to expressing complex queries on a set of data using the new Stream API instruction.

"What is a stream?" This is a new feature of Java 8. Its features are similar to collections, but there are several obvious advantages that allow us to use new programming styles. First of all, if you have used database query languages ​​such as SQL, you will find that a query statement written in a few lines of code is long to write if it is replaced with Java. Java 8 streams support this kind of concise database query programming-but using Java syntax, you don't need to know the database! Second, the stream is designed so that it does not need to transfer all the data into the memory at the same time (or even no calculation at all), so that it can process the stream data that cannot be loaded into the computer's memory. However, Java 8 can do some optimization operations on streams that cannot be performed by collections. For example, it can combine several operations on the same stream to traverse the data only once instead of traversing it multiple times at a high cost. Even better, Java can automatically parallelize stream operations (collections are not possible).

"There is also functional programming, what is this?" Just like object-oriented programming, it is another programming style whose core is to use functions as values, as mentioned earlier when discussing Lambda.

The advantage of Java 8 is that it incorporates some of the best ideas in functional programming into the familiar Java syntax. With this excellent design choice, you can think of functional programming as an additional design pattern and idiom in Java 8, allowing you to write clearer and more concise code in less time. Think of your programming weapon arsenal with more weapons.

Of course, in addition to these conceptually great extensions to Java, we will also explain many other useful Java 8 features and updates, such as default methods, new Optional classes, CompletableFuture, and new date and time APIs.

The update to Java 9 includes a module system that supports reactive programming through the Flow API, and various other enhancements.

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The following will take you step by step from the table of contents and the main content to the Java actual combat second edition. Because the full text contains a lot of content, the editor will only take out some of the knowledge points to give you an introduction. The full version is needed. Partners, you can get them in the following ways! ! Hope this article will be liked by everyone! !

table of Contents

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

main content

This article is divided into six parts, namely: "Basic knowledge", "Functional data processing using streams", "Efficient programming using streams and Lambda", "Ubiquitous Java", "Improving Java's concurrency" and "Functional programming and The future evolution of Java". We strongly recommend that you read the contents of the first two parts in order, because many concepts require the previous chapters as a basis, and you can read the contents of the latter four parts in any order. Most chapters are accompanied by several quizzes to help you learn and master these content.

The first part is designed to help you get started with Java 8. After studying this part, you will have a full understanding of Lambda expressions, and be able to write concise and flexible code that can easily adapt to changing needs.

Chapter 1 summarizes the main changes in Java (Lambda expressions, method references, streams, and default methods) to prepare for the following content.

Contents of this chapter

  • How did Java change again
  • The ever-changing background of computing applications
  • Pressure for Java improvement
  • Core new features of Java 8 and Java 9

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 2 introduces behavior parameterization, which is a software development model that Java 8 relies heavily on, and is the main reason for introducing Lambda expressions.

Contents of this chapter

  • Responding to changing needs
  • Behavior parameterization
  • Anonymous class
  • Lambda expression preview
  • Real-world examples: Comparator, Runnable and GUI

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 3 gives a comprehensive introduction to Lambda expressions and method references, and each step provides code examples and tests.

Contents of this chapter

  • Peeping Leopard in Lambda Tube
  • Where and how to use Lambda
  • Surround execution mode
  • Functional interface, type inference
  • Method reference
  • Lambda composite

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The second part discusses the new Stream API in detail. With the Stream API, you will be able to write powerful code to process data in a declarative manner. After studying this part, you will fully understand what streams are and how to use them in Java applications to process data sets concisely and efficiently.

Chapter 4 introduces the concept of streams and explains how they are similar to sets.

Contents of this chapter

  • What is flow
  • Collection and flow
  • Internal iteration and external iteration
  • Intermediate operation and terminal operation

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 5 discusses in detail the stream operations that can be used to express complex data processing queries. There will be many modes, such as filtering, slicing, searching, matching, mapping and reduction.

Contents of this chapter

  • Filter, slice and map
  • Find, match, and reduce
  • Use numerical streams such as numerical ranges
  • Create streams from multiple sources
  • Infinite flow

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 6 introduces the collector-a function of the Stream API that allows you to express more complex data processing queries.

Contents of this chapter

  • Create and use collectors with Collectors class
  • Reduce the data stream to a value
  • Summary: special cases of reduction
  • Data grouping and partitioning
  • Develop your custom collector

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 7 explores how streams can be automatically executed in parallel and take advantage of the multi-core architecture. In addition, you will learn several pitfalls to avoid in order to use parallel streams correctly and efficiently.

Contents of this chapter

  • Process data in parallel with parallel streams
  • Performance analysis of parallel streams
  • Branch/merge framework
  • Split stream using Spliterator

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The third part explores multiple topics in Java 8 and Java 9. The techniques in these topics can make your Java code more efficient and can help you improve your code base using modern programming idioms. The starting point of this part is to introduce advanced programming ideas, the rest of the book does not rely on this.

Chapter 8 is new in this edition. As a Java programmer, if you don't know or have not used the Collection API, you are too ignorant. Almost every Java application will use Collection more or less. Through the study of the previous chapters, you have seen how powerful it is to combine Collection API and Stream API to construct data processing queries. However, the Collection API also has various unsatisfactory places, which make it cumbersome to use and often error-prone.

Through this chapter, you will learn about the new features of the Collection API in Java 8 and Java 9, which can make your programming work more efficient. First, we will introduce the newly introduced collection factory in Java 9, which can greatly simplify the process of creating small-scale List, Set and Map. Next, I will introduce how to use the enhanced features of Java 8 to remove or replace elements in List and Set. Finally, I will learn some new methods of processing Map

Contents of this chapter

  • How to use the collection factory
  • Learn to use new idiomatic patterns to deal with List and Set
  • Learn to process Maps through idiomatic patterns

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 9 discusses how to use the new features of Java 8 and some tips to improve your existing code. In addition, this chapter also discusses some important software development techniques, such as design patterns, refactoring, testing, and debugging.

Through the first eight chapters of this article, we learned about the power of Lambda and Stream API. You may mainly use these features in the code of a new project. If you are creating a brand-new Java project, this is an excellent time to pack lightly and quickly apply new features to the project. Unfortunately, in most cases you will not have the opportunity to start a brand new project from scratch. In many cases, what you have to deal with is legacy code written with old Java interfaces.

These are the topics to be discussed in this chapter. We will introduce several methods to help you refactor the code to adapt to the use of Lambda expressions, so that the code you maintain has better readability and flexibility. In addition, several popular object-oriented design patterns will be discussed, including strategy pattern, template method pattern, observer pattern, responsibility chain pattern, and factory pattern, which become more concise when combined with Lambda expressions. Case. Finally, I will introduce how to test and debug the code using Lambda expressions and Stream API.

Contents of this chapter

  • How to refactor code using Lambda expressions
  • The influence of lambda expressions on object-oriented design patterns
  • Lambda expression testing
  • How to debug code using Lambda expressions and Stream API

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 10 is also new in this edition, introducing the idea of ​​implementing APIs based on domain-specific languages ​​(DSL). This is not only a powerful API design method, but it is becoming more and more popular. There are already APIs in Java that are implemented in this mode, such as the Comparator, Stream, and Collector interfaces.

Contents of this chapter

  • Domain-specific language (domain-specifc language, DSL) and its form
  • What are the advantages and disadvantages of adding a DSL to your API
  • In addition to a simple Java-based DSL, what other domain-specific languages ​​are available for the JVM
  • Learn domain-specific languages ​​from modern Java interfaces and classes
  • What are the patterns and techniques for efficiently implementing a Java-based DSL
  • How common Java libraries and tools use these patterns

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The fourth part introduces multiple new features in Java 8 and Java 9. These features help programmers write code with half the effort and make the program more stable and reliable. Let's start with the two new APIs in Java 8.

Chapter 11 introduces the java.util.Optional class, which allows you to design better APIs and reduce null pointer exceptions.

Contents of this chapter

  • Problems caused by null references, and why you should avoid null references
  • From null to Optional: Rewrite your domain model in a null-safe way
  • Let Optional shine: remove the check for null in the code
  • Several methods to read possible values ​​in Optional
  • Rethinking of possible missing values

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 12 discusses the new date and time API, which is a big improvement over the previous error-prone API when dealing with dates and times.

Contents of this chapter

  • Why do you need to introduce a new date and time library in Java 8
  • Show date and time for both human and machine
  • Define a measure of time
  • Manipulate, format and parse dates
  • Handle different time zones and calendars

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 13 discusses what the default methods are, how to use them to evolve the API in a compatible way, some practical application patterns, and the rules for effectively using the default methods.

Contents of this chapter

  • What is the default method
  • How to improve the API in a compatible way
  • Usage mode of the default method
  • Parsing rules

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 14 is new in this edition and discusses Java's module system-it is a major improvement in Java 9, enabling large systems to be modularized in a documented and executable manner, rather than simply putting a bunch of packages Stacked in disorder.

Contents of this chapter

  • Motivation to advance the road to Java modularity
  • The main structure of the module: module declaration and requires and exports instructions
  • Automatic module for Java archive files (JAR)
  • Modularity and JDK library
  • Use Maven to build multiple modules
  • Overview of module directives other than requirements and exports

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The fifth part discusses how to use Java's advanced features to build concurrent programs-note that we are not discussing the concurrent processing of streams introduced in Chapters 6 and 7.

Chapter 15 is new in this edition. It introduces the idea of ​​asynchronous API from a macro perspective, including Future and the "publish-subscribe" protocol behind reactive programming (encapsulated in the Flow API of Java 9).

Contents of this chapter

  • Threads, Futures, and the evolutionary power that drives Java to support richer concurrent APIs
  • Asynchronous API
  • View Concurrent Computing from the Perspective of "Wireframe and Pipeline"
  • Use CompletableFuture combiner to dynamically connect wireframes
  • The "publish-subscribe" protocol that forms the basis of the Java 9 reactive programming Flow API
  • Reactive programming and reactive systems

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 16 discusses CompletableFuture, which allows you to express complex asynchronous calculations in a declarative manner, thereby parallelizing the design of StreamAPI.

Contents of this chapter

  • Create an asynchronous calculation and get the calculation result
  • Use non-blocking operations to increase throughput
  • Design and implement asynchronous API
  • How to use synchronous APIs asynchronously
  • How to pipeline and merge two or more asynchronous operations
  • How to handle the completion status of asynchronous operations

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 17 is also new in this edition. It introduces the Flow API of Java 9 in detail and provides practical code analysis of reactive programming.

Contents of this chapter

  • What is reactive programming and the principles of reactive declaration
  • Application-level and system-level reactive programming
  • An example of using reactive stream and Java 9 Flow API
  • A widely used reactive library-RxJava
  • How to use RxJava to convert and integrate multiple reactive streams
  • How to use a marble diagram to visually record operations on reactive streams

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

The sixth part is the last part of the book. We will talk about how to write efficient functional programs in Java and compare the functions of Java with Scala.

Chapter 18 is a complete functional programming tutorial that will introduce some terminology and explain how to write functional style programs in Java 8.

Contents of this chapter

  • Why do functional programming
  • What is functional programming
  • Declarative programming and referential transparency
  • Guidelines for writing functional Java
  • Iteration and recursion

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 19 covers more advanced functional programming techniques, including higher-order functions, currying, persistent data structures, delayed lists, and pattern matching. This chapter provides not only the actual techniques that can be used in the code base, but also the academic knowledge that will make you a more knowledgeable programmer.

Contents of this chapter

  • First-class members, high-level methods, currying and local applications
  • Persistent data structure
  • Delay calculation and delay list when generating Java Stream
  • Pattern matching and how to apply it in Java
  • Referential transparency and caching

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 20 will compare the functions of Java and Scala. Scala, like Java, is a language implemented on the JVM. It has developed rapidly in recent years and has threatened some aspects of Java in the programming language ecosystem.

Contents of this chapter

  • What is Scala language
  • How do Java and Scala come together
  • What are the differences between functions in Scala and functions in Java
  • Classes and traits

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

Chapter 21 will review this journey of learning Java 8 and slowly moving towards functional programming. In addition, we will also guess what enhancements and new features may appear in the future after the small features added in Java 8, 9 and 10.

Contents of this chapter

  • New features of Java 8 and its subversive influence on programming style
  • Brand new Java 9 module system
  • Java increment every six months-release life cycle
  • Forms the first incremental release of Java 10
  • What else might be new in future Java versions

The java evangelist exhausted 20 years of skill, finishing the java actual combat second edition document

 

This [Java Actual Combat 2nd Edition] has a total of 615 pages. Because there is too much content, I won’t introduce too much here. If you need a full version, you can get it in the following ways! ! !

I hope this article can help you learn, read carefully and be able to use it flexibly, and strive to achieve proficiency. I also hope that this article will be liked by everyone!

Guess you like

Origin blog.csdn.net/JavaBye/article/details/109342283