Java8概述
- Java 8 (又称为jdk 1.8) 是Java 语言开发的一个主要版本。
- Java 8 是oracle公司于2014年3月发布,可以看成是自Java 5 以来最具革命性的版本。Java 8为Java语言、编译器、类库、开发工具与JVM带来了大量新特性。

Java8新特性的好处
- 速度更快
- 代码更少(增加了新的语法:Lambda 表达式)
- 强大的Stream API
- 便于并行
- 最大化减少空指针异常:Optional
- Nashorn引擎,允许在JVM上运行JS应用
并行流与串行流
并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流。相比较串行的流,并行的流可以很大程度上提高程序的执行效率。
Java 8 中将并行进行了优化,我们可以很容易的对数据进行并行操作。Stream API 可以声明性地通过parallel() 与sequential() 在并行流与顺序流之间进行切换。
Lambda表达式的使用
Lambda 是一个匿名函数,我们可以把Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。使用它可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。
/*** Lambda表达式的使用* <p>* 1.举例: (o1,o2) -> Integer.compare(o1,o2);* 2.格式:* -> :lambda操作符 或 箭头操作符* ->左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表)* ->右边:lambda体 (其实就是重写的抽象方法的方法体)* <p>* 3.Lambda表达式的使用:(分为6种情况介绍)* 总结:* ->左边:lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只有一个参数,其一对()也可以省略* ->右边:lambda体应该使用一对{}包裹;如果lambda体只有一条执行语句(可能是return语句),省略这一对{}和return关键字*/public class LambdaTest {// 语法格式一:无参,无返回值@Testpublic void test1() {Runnable r1 = new Runnable() {@Overridepublic void run() {System.out.println("普通表达式!!!");}};r1.run();Runnable r2 = () -> {System.out.println("Lambda表达式!!!");};r2.run();}//语法格式二:Lambda 需要一个参数,但是没有返回值。@Testpublic void test2() {Consumer<String> con1 = new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s);}};con1.accept("普通表达式!!!");Consumer<String> con2 = (String s) -> {System.out.println(s);};con2.accept("Lambda表达式!!!");// 由于前面写了String泛型,此时可以省略String,自动进行类型推断Consumer<String> con3 = (s) -> {System.out.println(s);};con3.accept("Lambda表达式!!!");// 只有一个参数,也可以省略()括号Consumer<String> con4 = s -> {System.out.println(s);};con4.accept("Lambda表达式!!!");// 如果只有一条语句,可以省略{},但此时return也要一起省略Consumer<String> con5 = s -> {System.out.println(s);};con5.accept("Lambda表达式!!!");}}
函数式(Functional)接口
函数式接口的介绍
/** 4.Lambda表达式的本质:作为函数式接口的实例** 5. 如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口。我们可以在一个接口上使用 @FunctionalInterface 注解,* 这样做可以检查它是否是一个函数式接口。**//*** 自定义函数式接口*/public interface MyInterFace {void method();// void method2();}
注意是一个抽象方法,如果其他方法都有实现的话,那么式函数式接口。
- 在java.util.function包下定义了Java 8 的丰富的函数式接口
- Java从诞生日起就是一直倡导“一切皆对象”,在Java里面面向对象(OOP)编程是一切。但是随着python、scala等语言的兴起和新技术的挑战,Java不得不做出调整以便支持更加广泛的技术要求,也即java不但可以支持OOP还可以支持OOF(面向函数编程)
- 在函数式编程语言当中,函数被当做一等公民对待。在将函数作为一等公民的编程语言中,Lambda表达式的类型是函数。但是在Java8中,有所不同。在Java8中,Lambda表达式是对象,而不是函数,它们必须依附于一类特别的对象类型——函数式接口。
- 简单的说,在Java8中,Lambda表达式就是一个函数式接口的实例。这就是Lambda表达式和函数式接口的关系。也就是说,只要一个对象是函数式接口的实例,那么该对象就可以用Lambda表达式来表示。
- 所以以前用匿名实现类表示的现在都可以用Lambda表达式来写。
Java内置的函数式接口介绍及使用举例

测试:
/*** java内置的4大核心函数式接口* <p>* 消费型接口 Consumer<T> void accept(T t)* 供给型接口 Supplier<T> T get()* 函数型接口 Function<T,R> R apply(T t)* 断定型接口 Predicate<T> boolean test(T t)*/public class LambdaTest2 {@Testpublic void test1() {List<String> list = Arrays.asList("长安", "上京", "江南", "渝州", "凉州", "兖州");List<String> stringList1 = filterString(list, new Predicate<String>() {@Overridepublic boolean test(String s) {return s.contains("州");}});System.out.println(stringList1);List<String> stringList = filterString(list, s -> s.contains("京")); // Lambda表达式System.out.println(stringList);}//根据给定的规则,过滤集合中的字符串。此规则由Predicate的方法决定public List<String> filterString(List<String> list, Predicate<String> pre) {ArrayList<String> filterList = new ArrayList<>();for (String s : list) {if (pre.test(s)) {filterList.add(s);}}return filterList;}public void happyTime(double money, Consumer<Double> con) {con.accept(money);}@Testpublic void test() {happyTime(30, new Consumer<Double>() {@Overridepublic void accept(Double aDouble) {System.out.println("熬夜太累了,点个外卖,价格为:" + aDouble);}});System.out.println("+++++++++++++++++++++++++");//Lambda表达式写法happyTime(20, money -> System.out.println("加个鸡腿!!!"));}}
方法引用
方法引用与构造器引用
- 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
- 方法引用可以看做是Lambda表达式深层次的表达。换句话说,方法引用就是Lambda表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是Lambda表达式的一个语法糖。
- 要求:实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!
- 格式:使用操作符“::” 将类(或对象) 与方法名分隔开来。
- 如下三种主要使用情况:
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
/*** 方法引用的使用** 1.使用情境:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!** 2.方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以* 方法引用,也是函数式接口的实例。** 3. 使用格式: 类(或对象) :: 方法名** 4. 具体分为如下的三种情况:* 情况1 对象 :: 非静态方法* 情况2 类 :: 静态方法** 情况3 类 :: 非静态方法** 5. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的* 形参列表和返回值类型相同!(针对于情况1和情况2)*/public class MethodRefTest {// 情况一:对象 :: 实例方法// Consumer中的void accept(T t)// PrintStream中的void println(T t)@Testpublic void test() {Consumer<String> c1 = str -> System.out.println(str);c1.accept("兖州");System.out.println("+++++++++++++");PrintStream ps = System.out;Consumer<String> c2 = ps::println;c2.accept("xian");}// Supplier中的T get()// Employee中的String getName()@Testpublic void test2() {Employee emp = new Employee(004,"Nice",19,4200);Supplier<String> sk1 = () -> emp.getName();System.out.println(sk1.get());System.out.println("*******************");Supplier<String> sk2 = emp::getName;System.out.println(sk2.get());}// 情况二:类 :: 静态方法// Comparator中的int compare(T t1,T t2)// Integer中的int compare(T t1,T t2)@Testpublic void test3() {Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1, t2);System.out.println(com1.compare(21, 20));System.out.println("+++++++++++++++");Comparator<Integer> com2 = Integer::compare;System.out.println(com2.compare(15, 7));}// Function中的R apply(T t)// Math中的Long round(Double d)@Testpublic void test4() {Function<Double, Long> func = new Function<Double, Long>() {@Overridepublic Long apply(Double d) {return Math.round(d);}};System.out.println("++++++++++++++++++");Function<Double, Long> func1 = d -> Math.round(d);System.out.println(func1.apply(14.1));System.out.println("++++++++++++++++++");Function<Double, Long> func2 = Math::round;System.out.println(func2.apply(17.4));}// 情况三:类 :: 实例方法 (有难度)// Comparator中的int comapre(T t1,T t2)// String中的int t1.compareTo(t2)@Testpublic void test5() {Comparator<String> com1 = (s1,s2) -> s1.compareTo(s2);System.out.println(com1.compare("abc","abd"));System.out.println("++++++++++++++++");Comparator<String> com2 = String :: compareTo;System.out.println(com2.compare("abd","abm"));}// BiPredicate中的boolean test(T t1, T t2);// String中的boolean t1.equals(t2)@Testpublic void test6() {BiPredicate<String,String> pre1 = (s1, s2) -> s1.equals(s2);System.out.println(pre1.test("MON","MON"));System.out.println("++++++++++++++++++++");BiPredicate<String,String> pre2 = String :: equals;System.out.println(pre2.test("MON","MON"));}// Function中的R apply(T t)// Employee中的String getName();@Testpublic void test7() {Employee employee = new Employee(007, "Ton", 21, 8000);Function<Employee,String> func1 = e -> e.getName();System.out.println(func1.apply(employee));System.out.println("++++++++++++++++++++++++");Function<Employee,String> f2 = Employee::getName;System.out.println(f2.apply(employee));}}
构造器引用与数组引用的使用
/*** 一、构造器引用* 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。* 抽象方法的返回值类型即为构造器所属的类的类型* <p>* 二、数组引用* 可以把数组看做是一个特殊的类,则写法与构造器引用一致。*/public class MethodRefTest1 {//构造器引用//Supplier中的T get()//Employee的空参构造器:Employee()@Testpublic void test() {Supplier<Employee> sup = new Supplier<Employee>() {@Overridepublic Employee get() {return new Employee();}};System.out.println("+++++++++++++++++++");Supplier<Employee> sk1 = () -> new Employee();System.out.println(sk1.get());System.out.println("+++++++++++++++++++");Supplier<Employee> sk2 = Employee::new;System.out.println(sk2.get());}//Function中的R apply(T t)@Testpublic void test2() {Function<Integer, Employee> f1 = id -> new Employee(id);Employee employee = f1.apply(7793);System.out.println(employee);System.out.println("+++++++++++++++++++");Function<Integer, Employee> f2 = Employee::new;Employee employee1 = f2.apply(4545);System.out.println(employee1);}//BiFunction中的R apply(T t,U u)@Testpublic void test3() {BiFunction<Integer, String, Employee> f1 = (id, name) -> new Employee(id, name);System.out.println(f1.apply(2513, "Fruk"));System.out.println("*******************");BiFunction<Integer, String, Employee> f2 = Employee::new;System.out.println(f2.apply(9526, "Bon"));}//数组引用//Function中的R apply(T t)@Testpublic void test4() {Function<Integer, String[]> f1 = length -> new String[length];String[] arr1 = f1.apply(7);System.out.println(Arrays.toString(arr1));System.out.println("+++++++++++++++++++");Function<Integer, String[]> f2 = String[]::new;String[] arr2 = f2.apply(9);System.out.println(Arrays.toString(arr2));}}
Stream流
- Java8中有两大最为重要的改变。第一个是Lambda 表达式;另外一个则是Stream API。
- Stream API ( java.util.stream)把真正的函数式编程风格引入到Java中。这是目前为止对Java类库最好的补充,因为Stream API可以极大提供Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
- Stream 是Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,就类似于使用SQL 执行的数据库查询。也可以使用Stream API 来并行执行操作。简言之,Stream API 提供了一种高效且易于使用的处理数据的方式。
- 为什么要使用Stream API
- 实际开发中,项目中多数数据源都来自于Mysql,Oracle等。但现在数据源可以更多了,有MongDB,Radis等,而这些NoSQL的数据就需要Java层面去处理。
- Stream 和Collection 集合的区别:Collection 是一种静态的内存数据结构,而Stream 是有关计算的。前者是主要面向内存,存储在内存中,后者主要是面向CPU,通过CPU 实现计算。
/*** 1.Stream关注的是对数据的运算,与CPU打交道* 集合关注的是数据的存储,与内存打交道** 2.* ①Stream 自己不会存储元素。* ②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。* ③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行** 3.Stream 执行流程* ① Stream的实例化* ② 一系列的中间操作(过滤、映射、...)* ③ 终止操作** 4.说明:* 4.1 一个中间操作链,对数据源的数据进行处理* 4.2 一旦执行终止操作,就执行中间操作链,并产生结果。之后,不会再被使用*/

测试环境
实体类
public class Employee {private int id;private String name;private int age;private double salary;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Employee() {System.out.println("Employee().....");}public Employee(int id) {this.id = id;System.out.println("Employee(int id).....");}public Employee(int id, String name) {this.id = id;this.name = name;}public Employee(int id, String name, int age, double salary) {this.id = id;this.name = name;this.age = age;this.salary = salary;}@Overridepublic String toString() {return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';}@Overridepublic boolean equals(Object o) {if (this == o)return true;if (o == null || getClass() != o.getClass())return false;Employee employee = (Employee) o;if (id != employee.id)return false;if (age != employee.age)return false;if (Double.compare(employee.salary, salary) != 0)return false;return name != null ? name.equals(employee.name) : employee.name == null;}@Overridepublic int hashCode() {int result;long temp;result = id;result = 31 * result + (name != null ? name.hashCode() : 0);result = 31 * result + age;temp = Double.doubleToLongBits(salary);result = 31 * result + (int) (temp ^ (temp >>> 32));return result;}}
/*** 提供用于测试的数据*/public class EmployeeData {public static List<Employee> getEmployees() {List<Employee> list = new ArrayList<>();list.add(new Employee(1001, "马化腾", 34, 6000.38));list.add(new Employee(1002, "马云", 12, 9876.12));list.add(new Employee(1003, "刘强东", 33, 3000.82));list.add(new Employee(1004, "雷军", 26, 7657.37));list.add(new Employee(1005, "李彦宏", 65, 5555.32));list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));list.add(new Employee(1007, "任正非", 26, 4333.32));list.add(new Employee(1008, "扎克伯格", 35, 2500.32));return list;}}
Stream的实例化
/*** 测试Stream的实例化*/public class StreamAPITest {//创建 Stream方式一:通过集合@Testpublic void test() {List<Employee> employees = EmployeeData.getEmployees();// default Stream<E> stream() : 返回一个顺序流Stream<Employee> stream = employees.stream();// default Stream<E> parallelStream() : 返回一个并行流Stream<Employee> parallelStream = employees.parallelStream();}//创建 Stream方式二:通过数组@Testpublic void test2() {int[] arr = new int[]{1, 2, 3, 4, 5, 6};//调用Arrays类的static <T> Stream<T> stream(T[] array): 返回一个流IntStream stream = Arrays.stream(arr);Employee e1 = new Employee(1001, "Hom");Employee e2 = new Employee(1002, "Nut");Employee[] arr1 = new Employee[]{e1, e2};Stream<Employee> stream1 = Arrays.stream(arr1);}//创建 Stream方式三:通过Stream的of()@Testpublic void test3() {Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);}//创建 Stream方式四:创建无限流@Testpublic void test4() {// 迭代// public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)//遍历前10个偶数Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);// 生成// public static<T> Stream<T> generate(Supplier<T> s)Stream.generate(Math::random).limit(10).forEach(System.out::println);}}
Stream的中间操作:筛选与切片
多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。

/*** 测试Stream的中间操作*/public class StreamAPITest2 {//1-筛选与切片@Testpublic void test() {List<Employee> list = EmployeeData.getEmployees();// filter(Predicate p)——接收 Lambda , 从流中排除某些元素。Stream<Employee> stream = list.stream();//练习:查询员工表中薪资大于7000的员工信息stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println);System.out.println("+++++++++++++++++++++++");// limit(n)——截断流,使其元素不超过给定数量。list.stream().limit(3).forEach(System.out::println);System.out.println("+++++++++++++++++++++++");// skip(n) —— 跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补list.stream().skip(3).forEach(System.out::println);System.out.println("+++++++++++++++++++++++");// distinct()——筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素list.add(new Employee(1013, "李飞", 42, 8500));list.add(new Employee(1013, "李飞", 41, 8200));list.add(new Employee(1013, "李飞", 28, 6000));list.add(new Employee(1013, "李飞", 39, 7800));list.add(new Employee(1013, "李飞", 40, 8000));// System.out.println(list);list.stream().distinct().forEach(System.out::println);}}
Stream的中间操作:映射

/*** 测试Stream的中间操作*/public class StreamAPITest3 {//2-映射@Testpublic void test2() {// map(Function f)——接收一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每个元素上,并将其映射成一个新的元素。List<String> list = Arrays.asList("aa", "bb", "cc", "dd");list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);// 练习1:获取员工姓名长度大于3的员工的姓名。List<Employee> employees = EmployeeData.getEmployees();employees.stream().map(e -> e.getName()).filter(name -> name.length() > 3).forEach(System.out::println);System.out.println();//练习2:Stream<Stream<Character>> streamStream = list.stream().map(StreamAPITest3::fromStringToStream);streamStream.forEach(s -> {s.forEach(System.out::println);});System.out.println("++++++++++++++++++++++");// flatMap(Function f)——接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。Stream<Character> characterStream = list.stream().flatMap(StreamAPITest3::fromStringToStream);characterStream.forEach(System.out::println);}//将字符串中的多个字符构成的集合转换为对应的Stream的实例public static Stream<Character> fromStringToStream(String str) {//aaArrayList<Character> list = new ArrayList<>();for (Character c : str.toCharArray()) {list.add(c);}return list.stream();}@Testpublic void test3() {ArrayList list1 = new ArrayList();list1.add(25);list1.add(33);list1.add(14);ArrayList list2 = new ArrayList();list2.add(51);list2.add(23);list2.add(61);// list1.add(list2);list1.addAll(list2);System.out.println(list1);}}
Stram流的中间操作:排序

public class StreamAPITest4 {//3-排序@Testpublic void test4() {// sorted()——自然排序List<Integer> list = Arrays.asList(25, 45, 36, 12, 85, 64, 72, -95, 4);list.stream().sorted().forEach(System.out::println);//抛异常,原因:Employee没有实现Comparable接口// List<Employee> employees = EmployeeData.getEmployees();// employees.stream().sorted().forEach(System.out::println);// sorted(Comparator com)——定制排序List<Employee> employees = EmployeeData.getEmployees();employees.stream().sorted((e1, e2) -> {int ageValue = Integer.compare(e1.getAge(), e2.getAge());if (ageValue != 0) {return ageValue;} else {return -Double.compare(e1.getSalary(), e2.getSalary());}}).forEach(System.out::println);}}
Stream流的中间操作:匹配和查询

public class StreamAPITest5 {//1-匹配与查找@Testpublic void test() {List<Employee> employees = EmployeeData.getEmployees();// allMatch(Predicate p)——检查是否匹配所有元素。// 练习:是否所有的员工的年龄都大于18boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 23);System.out.println(allMatch);// anyMatch(Predicate p)——检查是否至少匹配一个元素。// 练习:是否存在员工的工资大于 10000boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 9000);System.out.println(anyMatch);// noneMatch(Predicate p)——检查是否没有匹配的元素。// 练习:是否存在员工姓“马”boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("马"));System.out.println(noneMatch);// findFirst——返回第一个元素Optional<Employee> employee = employees.stream().findFirst();System.out.println(employee);// findAny——返回当前流中的任意元素Optional<Employee> employee1 = employees.parallelStream().findAny(); // 转化为并行流System.out.println(employee1);}@Testpublic void test2() {List<Employee> employees = EmployeeData.getEmployees();// count——返回流中元素的总个数long count = employees.stream().filter(e -> e.getSalary() > 4500).count();System.out.println(count);// max(Comparator c)——返回流中最大值// 练习:返回最高的工资:Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary());Optional<Double> maxSalary = salaryStream.max(Double::compare);System.out.println(maxSalary);// min(Comparator c)——返回流中最小值// 练习:返回最低工资的员工Optional<Employee> employee = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));System.out.println(employee);System.out.println();// forEach(Consumer c)——内部迭代employees.stream().forEach(System.out::println);//使用集合的遍历操作employees.forEach(System.out::println);}}
Stream的终止操作:归约

备注:map 和reduce 的连接通常称为map-reduce 模式,因Google 用它来进行网络搜索而出名。
public class StreamAPITest6 {//2-归约@Testpublic void test3() {// reduce(T identity, BinaryOperator)——可以将流中元素反复结合起来,得到一个值。返回 T// 练习1:计算1-10的自然数的和List<Integer> list = Arrays.asList(72, 25, 32, 34, 43, 56, 81, 15, 29, 71);Integer sum = list.stream().reduce(0, (a, b) -> Integer.sum(a, b));System.out.println(sum);// reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。返回 Optional<T>// 练习2:计算公司所有员工工资的总和List<Employee> employees = EmployeeData.getEmployees();Stream<Double> salaryStream = employees.stream().map(Employee::getSalary);// Optional<Double> sumMoney = salaryStream.reduce(Double::sum);Optional<Double> sumMoney = salaryStream.reduce((d1, d2) -> d1 + d2);System.out.println(sumMoney.get());}}
Stream的终止操作:收集

public class StreamAPITest7 {//3-收集@Testpublic void test4() {// collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法// 练习1:查找工资大于6000的员工,结果返回为一个List或SetList<Employee> employees = EmployeeData.getEmployees();List<Employee> employeeList = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());employeeList.forEach(System.out::println);System.out.println("++++++++++++++++++");Set<Employee> employeeSet = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet());employeeSet.forEach(System.out::println);}}
Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到List、Set、Map)。
Collectors实用类提供了很多静态方法,可以方便地创建常见收集器实例,具体方法与实例如下表:


