2017年3月11日星期六

Java 8 in Action -- Chapter 1. Java 8: why should you care?

1.  The changes to Java 8 enable you to write more concise code and provides a simpler use of multicore processors.

2.  The three programming concepts added to Java 8 are stream processing, passing code to methods with behavior parameterization and parallelism with no shared mutable data.

3.  Java 8 provides a new API (called Streams) that supports many parallel operations to process data and resembles the way you might think in database query languages—you express what you want in a higher-level manner, and the implementation (here the Streams library) chooses the best low-level execution mechanism.

4.  The Java 8 feature of passing code to methods (and also being able to return it and incorporate it into data structures) also provides access to a whole range of additional techniques that are commonly referred to as functional-style programming.

5.  A stream is a sequence of data items that are conceptually produced one at a time.

6.  Java 8 adds a Streams API in java.util.stream. Stream<T> is a sequence of items of type T. You can think of it as a fancy iterator for now. The Streams API has many methods that can be chained to form a complex pipeline just like Unix commands are chained:

cat file1 file2  |  tr "[A-Z]"  "[a-z]"  |  sort  |  tail -3

which (supposing file1 and file2 contain a single word per line) prints the three words from the files that appear latest in dictionary order, after first translating them to lowercase.(Unix cat creates a stream by concatenating two files, tr translates the characters in a stream, sort sorts lines in a stream, and tail -3 gives the last three lines in a stream. )

7.  You can now program in Java 8 at a higher level of abstraction, structuring your thoughts of turning a stream of this into a stream of that. Java 8 can transparently run your pipeline of Stream operations on several CPU cores on disjoint parts of the input—this is parallelism almost for free instead of hard work using Threads.

8.  You must provide behavior passed to stream methods that is safe to execute concurrently on different pieces of the input. Typically this means writing code that doesn’t access shared mutable data to do its job. Sometimes these are referred to as pure functions or side-effect-free functions or stateless functions.

9.  Values which can’t be passed around during program execution, are second-class values.

10.  Java 8 introduces method references by :: syntax : File::isHidden

11.  As well as allowing (named) methods to be first-class values, Java 8 allows a richer idea of functions as values, including lambda (or anonymous functions). For example, you can now write (int x) -> x + 1 to mean “the function that, when called with argument x, returns the value x + 1.”

12.  The word predicate is often used in mathematics to mean something function-like that takes a value for an argument and returns true or false. Java 8 would also allow you to write Function<Apple,Boolean> but using Predicate<Apple> is more standard (and slightly more efficient because it avoids boxing a boolean into a Boolean).

13.  Streams API provides a very different way to process data in comparison to the Collections API. Using a collection, you’re managing the iteration process yourself. You need to iterate through each element one by one using a for-each loop and then process the elements. We call this way of iterating over data external iteration. In contrast, using the Streams API, you don’t need to think in terms of loops at all. The data processing happens internally inside the library. We call this idea internal iteration.

14.  Collections is mostly about storing and accessing data, whereas Streams is mostly about describing computations on data. The first design motivator is that there are many data processing patterns that occur over and over again and that would benefit from forming part of a library: filtering data based on a criterion, extracting data , or grouping data , and so on. The second motivator is that such operations can often be parallelized.

15.  There are actually two magic bullets for parallelism in Java. First, the library handles partitioning—breaking down a big stream into several smaller streams to be processed in parallel for you. Second, this parallelism almost for free from streams works only if the methods passed to library methods don’t interact, for example, by having mutable shared objects.

16.  Default methods are added to Java 8 largely to support library designers by enabling them to write more evolvable interfaces. An interface can now contain method signatures for which an implementing class doesn’t provide an implementation. The missing method bodies are given as part of the interface (hence default implementations) rather than in the implementing class.

17.  In Java 8 there’s an Optional<T> class that, if used consistently, can help you avoid NullPointer exceptions. It’s a container object that may or not contain a value. Optional<T> includes methods to explicitly deal with the case where a value is absent, and as a result you can avoid NullPointerException. In other words, it uses the type system to allow you to indicate when a variable is anticipated to potentially have a missing value.

18.  For more complex data types, pattern matching can express programming ideas more concisely compared to using if-then-else. Unfortunately, Java 8 doesn’t have full support for pattern matching. you can think of pattern matching as an extended form of switch that can decompose a data type into its components at the same time.

没有评论:

发表评论