java in action_Java8 in action

解决的问题:

behavior parameterization,即可以把一段code,逻辑作为参数传入;

这样做的目的,当然为了代码抽象和重用,把变化的逻辑抽象出去;

在java中,如果要实现behavior parameterization,需要通过传入类对象的方式,你首先要声明很多类,verbose

就算你用匿名类的方式,也会大大影响代码的可读性

所以lambda,是function programing的元素,可以将一个匿名函数作为参数传入

Lambdas

A lambda expression can be understood as a concise representation of an anonymous function that can be passed around

 Anonymous— We say anonymous because it doesn’t have an explicit name like a method would normally have: less to write and think about!

 Function— We say function because a lambda isn’t associated with a particular class like a method is. But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.

 Passed around— A lambda expression can be passed as argument to a method or stored in a variable.

 Concise— You don’t need to write a lot of boilerplate like you do for anonymous classes.

java in action_Java8 in action_第1张图片

在什么地方可以使用lamda?

So where exactly can you use lambdas? You can use a lambda expression in the context of a functional interface.

In a nutshell, a functional interface is an interface that specifiesexactly one abstract method.

java in action_Java8 in action_第2张图片

可以看到lambda在用法上和匿名类是等价的

Function descriptor

在java8中,有多少种functional interface,

其中function description表示functional的signature,比如Consumer,参数T,无返回值

java in action_Java8 in action_第3张图片

Predicate,典型的filter场景

The java.util.function.Predicate interface defines an abstract method named test that accepts an object of generic type T and returns a boolean.

Predicate nonEmptyStringPredicate = (String s) -> !s.isEmpty();

List nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);

Consumer,消费者场景

The java.util.function.Consumer interface defines an abstract method named accept that takes an object of generic type T and returns no result (void).

java in action_Java8 in action_第4张图片

Function,transform场景

The java.util.function.Function interface defines an abstract method named apply that takes an object of generic type T as input and returns an object of generic type R.

java in action_Java8 in action_第5张图片

Type inference

用于编译器会做类型check和推导,所以你可以省略参数类型

java in action_Java8 in action_第6张图片

Using local variables,弱版闭包

int portNumber = 1337;

Runnable r= () -> System.out.println(portNumber)

看到lambda,使用临时变量

They’re called capturing lambdas

和闭包的不同是,他有限制,

Lambdas are allowed to capture (that is, to reference in their bodies) instance variables and static variables without restrictions.

But local variables have to be explicitly declared final or are effectively final.

In other words, lambda expressions can capture local variables that are assigned to them only once.

34a205b2b76d3a03c18d96e696d70917.png

这样会报错,因为你多次修改

我的理解,其实你改不改,对于Java的实现是没有问题,反正都是在你引用的时候,产生一份copy

但是如果你修改了值,容易产生歧义,你会期望引用的值变成你改的值,其实不会

Method references

Method references let you reuse existing method definitions and pass them just like lambdas.

In some cases they appear more readable and feel more natural than using lambda expressions.

java in action_Java8 in action_第7张图片

You can think of method references as syntactic sugar for lambdas that refer only to a single method because you write less to express the same thing.

一种用于更简单的表示lambda的语法糖

Recipe for constructing method references

There are three main kinds of method references:

1. A method reference to a static method (for example, the method parseInt of Integer, written Integer::parseInt)

2. A method reference to an instance method of an arbitrary type (for example, the method length of a String, written String::length)

3. A method reference to an instance method of an existing object (for example, suppose you have a local variable expensiveTransaction that holds an object of type Transaction, which supports an instance method getValue; you can write expensiveTransaction::getValue)

java in action_Java8 in action_第8张图片

Constructor references

You can create a reference to an existing constructor using its name and the keyword new as follows: ClassName::new. It works similarly to a reference to a static method.

java in action_Java8 in action_第9张图片

注意,Apple::new,不是调用new,这个是产生一个function interface,supplier

对于带参数的构造函数,生成的就是function

java in action_Java8 in action_第10张图片

Useful methods to compose lambda expressions

Composing Comparators

8fb879544662c3d4db58042544eed613.png

Composing Predicates

fadb1836599c1e5ceb41a41d8ac1d99e.png

ffd98370ffe3788f5af446e52f228135.png

Composing Functions

java in action_Java8 in action_第11张图片

55737af4f496098b740d7cf3e4ae9d57.png

Streams API

To summarize, the Streams API in Java 8 lets you write code that’s

 Declarative— More concise and readable

 Composable— Greater flexibility

 Parallelizable— Better performance

说白,就是以function programming的方式去处理collection,不需要显式的去写迭代,所以declarative

并且,可以透明的处理并行化问题

java in action_Java8 in action_第12张图片

Streams vs. collections

java in action_Java8 in action_第13张图片

Working with streams

To summarize, working with streams in general involves three items:

 A data source (such as a collection) to perform a query on

 A chain of intermediate operations that form a stream pipeline

 A terminal operation that executes the stream pipeline and produces a result

Intermediate operations such as filter or sorted return another stream as the return type.

This allows the operations to be connected to form a query. What’s important is that intermediate operations don’t perform any processing until a terminal operation is invoked on the stream pipeline—they’re lazy.

java in action_Java8 in action_第14张图片java in action_Java8 in action_第15张图片

Numeric streams

Mapping to a numeric stream

The most common methods you’ll use to convert a stream to a specialized version are mapToInt, mapToDouble, and mapToLong.

d439fe61a04ccf11908a21c2b9d61660.png

这样可以直接用sum

和下面的写法比较一下,

java in action_Java8 in action_第16张图片

Converting back to a stream of objects

java in action_Java8 in action_第17张图片

Numeric ranges

46e3c3064cb7a2d765ccb9fb3e6cd427.png

Building streams

Streams from values

Stream stream = Stream.of("Java 8 ", "Lambdas ", "In ", "Action");

Stream emptyStream = Stream.empty();

Streams from arrays

da0ee8157ec36166f250d4fdd7d26c35.png

Streams from files

java in action_Java8 in action_第18张图片

Streams from functions: creating infinite streams

9d790fb0080ac3664e7e23064c4fb7ba.pngb0be8362abd1c36fd9c1cdc9087bdecd.png

Collecting data with streams

Collectors as advanced reductions

List transactions = transactionStream.collect(Collectors.toList());

java in action_Java8 in action_第19张图片java in action_Java8 in action_第20张图片java in action_Java8 in action_第21张图片

Parallel data processing and performance

通过parallel来打开并行

java in action_Java8 in action_第22张图片

sequential(),可以切换到串行

这里的并行是由fork/join来实现的

fork/join framework

The fork/join framework was designed to recursively split a parallelizable task into smaller tasks and then combine the results of each subtask to produce the overall result.

可以理解成local的map reduce

It’s an implementation of the ExecutorService interface, which distributes those subtasks to worker threads in a thread pool, called ForkJoinPool.

在java7中,我要这样写fork/join应用,比如一个简单的并行求和

java in action_Java8 in action_第23张图片

注意在compute中,我们会判断阈值,如果不满足,就不断的二分,并递归的调用

然后,这样调用

public static long forkJoinSum(longn) {long[] numbers = LongStream.rangeClosed(1, n).toArray();

ForkJoinTask task = newForkJoinSumCalculator(numbers);return newForkJoinPool().invoke(task);

}

java in action_Java8 in action_第24张图片

Spliterator

这是对fork逻辑的抽象

Java 8 already provides a default Spliterator implementation for all the data structures included in its Collections Framework.

Collections now implements the interface Spliterator, which provides a method spliterator.

在Java8中,所有Collections都是实现Spliterator接口

public interface Spliterator{boolean tryAdvance(Consumer super T>action);

SpliteratortrySplit();longestimateSize();intcharacteristics();

}

The algorithm that splits a Stream into multiple parts is a recursive process and proceeds as shown in figure 7.6.

java in action_Java8 in action_第25张图片

split的过程就是不断的调用trySplit

实现WordCount功能的Spliterator

java in action_Java8 in action_第26张图片java in action_Java8 in action_第27张图片

Default methods

Interface如果发生改变,增加function,那么所有实现该interface的类都需要修改以实现新的function,这个太烦躁了;

所以在Java8,给interface加了default methods

java in action_Java8 in action_第28张图片

Using Optional as a better alternative to null

大家是不是都被NullPointerException烦的不行

需要写出这样丑陋的代码

java in action_Java8 in action_第29张图片java in action_Java8 in action_第30张图片

Java 8 introduces a new class called java.util.Optional that’s inspired by the ideas of Haskell and Scala.

Object value = map.get("key");

写成,

Optional

也可以写成这样,

java in action_Java8 in action_第31张图片

区别就是of会抛异常

option支持的操作

java in action_Java8 in action_第32张图片

开始的例子,可以写成,

java in action_Java8 in action_第33张图片

你可能感兴趣的:(java,in,action)