reference 《java8实战》

第三章 lambda表达式

3.2 在哪里以及如何使用lambda表达式

3.2.1 函数式接口

3.4 使用函数式接口

3.4.1 Predicate

@FunctionalInterface
public interface Predicate<T>{
	boolean test(T t);
}

3.4.2 Consumer

@FunctionalInterface
public interface Consumer<T>{
	void accept(T t);
}

3.4.3 Function

@FunctionalInterface
public interface Function<T, R>{
	R apply(T t);
}

3.6 方法引用

3.6.1

  1. 指向静态方法的方法引用Integer::parseInt
  2. 指 向 任意类型实例方法 的方法引用String::length
  3. 指向现有对象的实例方法的方法引用expensiveTransaction::getValue

3.6.2 构造函数引用

Apple::new

第4章 引入流

4.1 流是什么

List<Dish> menu = Arrays.asList(
new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 300, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH) );
public class Dish {
	private final String name;
	private final boolean vegetarian;
	private final int calories;
	private final Type type;
	public Dish(String name, boolean vegetarian, int calories, Type type) {
		this.name = name;
		this.vegetarian = vegetarian;
		this.calories = calories;
		this.type = type;
	}
 
public String getName() {
 
return name;
 
}
 
public boolean isVegetarian() {
 
return vegetarian;
 
}
 
public int getCalories() {
 
return calories;
 
}
 
public Type getType() {
 
return type;
 
}
 
@Override
 
public String toString() {
 
return name;
 
}
public enum Type { MEAT, FISH, OTHER }
 
}

4.4 流操作

中间操作

终端操作

第5章 使用流

streamAPI

5.1 筛选和切片

名字方法名作用
filter筛选
distinct筛选各异的元素
limint截短流
skip跳过元素

5.2 映射

名字方法名作用
map对流中每一个元素应用函数
flatMap流的扁平化

5.3查找和匹配

5.3.2 检查谓词是否匹配所有元素

名字方法名作用
anyMatch检查谓词是否至少匹配一个元素
allMatch
noneMath

查找元素

名字方法作用
findAny
findFirst

5.4 规约

名字方法作用
reduce
Optional<Integer> max = numbers.stream().reduce(Integer::max);

第6章 用流搜集数据

6.5 收集器接口

Collector

public interface Collector<T, A, R> {
 
	Supplier<A> supplier();
	
	BiConsumer<A, T> accumulator();
	
	Function<A, R> finisher();
	
	BinaryOperator<A> combiner();
	
	Set<Characteristics> characteristics();
 
}
  • T是流中要收集的项目的泛型。
  • A是累加器的类型,累加器是在收集过程中用于累积部分结果的对象。
  • R是收集操作得到的对象(通常但并不一定是集合)的类型。

第9章 默认方法

接口中可以定义默认方法和静态方法。

 
public interface Sized {
	int size();
	default isEmpty(){
		return size == 0;
	}
}

可选方法

interface Iterator<T> {
	boolean hasNext();
	T next();
	default void remove() {
		throw new UnsupportedOperationException();
	}
}

解决冲突的规则

  • 类中的方法优先级最高 类或父类中声明的方法的优先级高于任何声明为默认方法的优 先级
  • 如果无法依据第一条进行判断,那么子接口的优先级更高:函数签名相同时,优先选择拥有最具体实现的默认方法的接口,即如果B继承了A,那么B就比A更加具体。
  • 最后,如果还是无法判断,继承了多个接口的类必须通过显式覆盖和调用期望的方法,显式地选择使用哪一个默认方法的实现。

第10章 用Optional取代null

Optional.empty();
 
 
Optional.of(car);
 
Optional.ofNullable(car);
 
 
String name = null;
if(insurance != null){
name = insurance.getName();
}


Optional<Insurance> optInsurance = Optional.ofNullable(insurance);
Optional<String> name = optInsurance.map(Insurance::getName);
orElse(T other)
 
orElseGet(Supplier<? extends T> other)是orElse方法的延迟调用版,Supplier
方法只有在Optional对象不含值时才执行调用。
 
ifPresent(Consumer<? super T>)
filter()如果值存在并且满足提供的谓词,就返回包含该值的 Optional 对象;否则返回一个空的
Optional 对象

第11章 CompletableFuture:组合式异步编程

第12章 新的日期和时间API

12.1 LocalDate、LocalTime、Instant、Duration 以及 Period

LocalDate
 
LocalTime
 
LocalDateTime

12.2 操纵、解析和格式化日期

Temporal接口

TemporalAdjuster接口

格式化以及解析日期时间对象

LocalDate date = LocalDate.of(2014, 3, 18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE); // 20140318
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2014-03-18
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date1 = LocalDate.of(2014, 3, 18);
String formattedDate = date1.format(formatter);
LocalDate date2 = LocalDate.parse(formattedDate, formatter);

12.3 处理不同的时区和历法