参考文献

  • Java攻略:Java常见问题的简单解法
  • Java8 实战

lambda表达式

  • 函数式接口:是一种包含单一抽象方法(single abstract method)的接口.类通过为接口中的所有方法提供实现来实现任何接口,这可以通过顶级类(top-level class),内部类甚至匿名内部类完成.

  • lambda表达式必须匹配接口中单一方法签名参数类型返回类型,这被称为与方法签名兼容.因此lambda表达式属于接口方法的实现,可以将其赋值给该接口类型的引用.

    • Java库中不存在名为lambda的类,lambda表达式只能被赋值给函数式接口引用.
  • lambda表达式在任何情况下都不能脱离上下文存在,上下文指定了将表达式给哪个函数式接口.

    • lambda表达式既可以是方法的参数,也可以是方法的返回类型,还可以被赋给引用.
    • 无论哪种情况,赋值类型必须为函数式接口.
  • lambda表达式的本质就是匿名函数,在定义和调用时不需要被赋予类型名或绑定到标识符.

方法引用

  • 使用**双冒号表示法(::)**将示例引用或类名与方法分开.

  • 语法

    • object::instanceMethod: 引用特定对象的实例方法.如System.out::println
    • Class::staticMethod: 引用静态方法,如Math::max
    • Class::instanceMethod: 调用特定类型的任意对象的实例方法,如String::length

构造函数引用

  • 在方法中使用new关键字
1
2
3
4
List<String> names = Arrays.asList("A","B","C");
List<Person> people = names.stream().map(name->new Person(name)).collect(Collectors.toList());
// ==>
List<Person> people = names.stream().map(Person::new).collect(Collectors.toList());
  • Person::new的作用是引用Person类的构造函数.与所有lambda表达式类似,有上下文决定执行哪个构造函数.由于上下文提供一个字符串,则使用单参数的String构造函数.

复制构造函数

  • 复制构造函数(copy constructor)传入一个Person参数,并返回一个具有相同特性的新Person.

    1
    2
    3
    public Person(Person p){
    this.name = p.name;
    }
    1
    final List<Person> people = Stream.of(before).map(Person::new).collect(Collectors.toList());
    • 若需要将流代码从原始实例中分离出来,复制构造函数将很有用.

可变参数构造函数

1
2
3
public Person(String... names){
this.name = Arrays.stream(names).collect(Collectors.joining(""));
}
1
2
3
4
names.stream()
.map(name->name.split(""))
.map(Person::new)
.collect(Collectors.toList());

数组

  • 构造函数引用也可以和数组一起使用.若希望采用实例的数组(Person[])而非列表,可以使用Stream接口定义的toArray方法

    1
    <A> A[] toArray(IntFunction<A[]> generator)
    • toArray方法采用A表示返回数组的泛型类型(generic type).数组包含流的元素,由所提供的generator函数创建.
    1
    2
    3
    Person[] people = names.stream()
    .map(Person::new)
    .toArray(Person::new);

异常处理

  • 任何函数式结构都不允许抛出受检异常(checked exception)

  • 若需要kanbda表达式抛出异常,有两种办法

    • 定一个自己的函数式接口,并声明受检异常

      1
      2
      3
      4
      5
      @FunctionalInterface
      public interface BufferReaderProcessor {
      String process(BufferedReader b) throws IOException;
      }
      BufferReaderProcessor p = (BufferedReader br) -> br.readLine();
    • 或者把lambda包在一个try-catch块中

      1
      2
      3
      4
      5
      6
      7
      Function<BufferedReader,String> f = (BufferedReader b) -> {
      try{
      return b.readLine();
      } catch(IOExecption e){
      throws new RuntimeException();
      }
      }

函数式接口

  • 创建只包含单一抽象方法的接口,并为其添加@FunctionalInterface注解.

java.util.function

  • java.util.function包中接口分为四类
    • Consumer消费型接口
    • Supplier供给型(生产者)接口
    • Predicate谓词型接口
    • Function功能型接口
函数式接口 方法名称 参数 返回值
Runnable run 无参数 无返回值
Function apply 1个参数 有返回值
Consumer accept 1个参数 无返回值
Supplier get 无参数 有返回值
BiConsumer accept 2个参数 无返回值
函数式接口 函数描述符 原始类型特化
Predicate<T> T->boolean IntPredicate,DoublePredicate,LongPredicate
Consumer<T> T->void IntConsumer,DoubleConsumer,LongConsumer
Function<T, R> T->R IntFunction<R>,IntToDoubleFunction,IntToLongFunction,
DoubleFunction<R>,
LongFunction<R>,LongToDoubleFunction,LongToIntFunction
ToIntFunction<T>,ToDoubleFunction<T>,ToLongFunction<T>
Supplier<T> ()->T BooleanSupplier,IntSupplier,DoubleSupplier,LongSupplier
UnaryOperator<T> T->T IntUnaryOperator,LongUnaryOperator,DoubleUnaryOperator
BinaryOperator<T> (T,T)->T IntBinaryOperator,DoubleBinaryOperator,LongBinaryOperator
BiPredicate<T, U> (T,U)->boolean
BiConsumer<T, U> (T,U)->void ObjIntConsumer<T>,ObjDoubleConsumer<T>,
ObjLongConsumer<T>
BiFunction<T, U, R> (T,U)->R ToIntFunction<T>,ToDoubleFunction<T>,ToLongFunction<T>

java.util.function.Consumer接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package java.util.function;

import java.util.Objects;

/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #accept(Object)}.
*
* @param <T> the type of the input to the operation
*
* @since 1.8
*/
@FunctionalInterface
public interface Consumer<T> {

/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);

/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}

java.util.function.Supplier接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package java.util.function;

/**
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {

/**
* Gets a result.
*
* @return a result
*/
T get();
}

java.util.function.Predicate接口

  • Predicate接口主要用于流的筛选.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package java.util.function;

import java.util.Objects;

/**
* Represents a predicate (boolean-valued function) of one argument.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since 1.8
*/
@FunctionalInterface
public interface Predicate<T> {

/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);

/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}

/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
default Predicate<T> negate() {
return (t) -> !test(t);
}

/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}

/**
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*
* @param <T> the type of arguments to the predicate
* @param targetRef the object reference with which to compare for equality,
* which may be {@code null}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}

/**
* Returns a predicate that is the negation of the supplied predicate.
* This is accomplished by returning result of the calling
* {@code target.negate()}.
*
* @param <T> the type of arguments to the specified predicate
* @param target predicate to negate
*
* @return a predicate that negates the results of the supplied
* predicate
*
* @throws NullPointerException if target is null
*
* @since 11
*/
@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
}

java.util.function.Function接口

  • Function接口包含的单一抽象方法为apply,它可以将T类型的泛型输入参数转换为R类型的泛型输出值.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package java.util.function;

import java.util.Objects;

/**
* Represents a function that accepts one argument and produces a result.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {

/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);

/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}

/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}

/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}

Optional

  • Optional是用于防范NullPointerException

创建Optional对象

声明一个空的Optional
1
final Optional<Object> empty = Optional.empty();
依据一个非空值创建Optional
1
final Optional<Person> person = Optional.of(test);
可接受nullOptional
1
final Optional<Person> test = Optional.ofNullable(person);

使用Optional对象

使用mapOptional对象中提取和转换值
1
final Optional<String> name = test.map(Person::getName);
1
2
final Optional<Employee> department = Optional.of(new Employee());
final Optional<String> s = department.map(Employee::getDepartment).map(Department::getFirstName);