1只要掌握以下3点:四大函数式接口、函数式接口常用操作、Stream流式常规操作 2 3/** 4 * @desc Java四大内置函数式接口 5 * Consumer<T>:消费型接口 6 * void accept(T t); 7 * Suppler<T>:供给型接口 8 * T get(); 9 * Function<T, R>:函数型接口 10 * R apply(T t); 11 * Predicate<T>:断言型接口 12 * boolean test(T t); 13 * @Author xw 14 * @Date 2019/9/9 15 */ 16public class JDKFuncInterfaceDemo { 17 public static void main(String[] args) { 18 // Consumer<T>:消费型接口 19 printConsumer("abcd", (x) -> System.out.println(x)); 20 // Suppler<T>:供给型接口; 21 // 案例:产生指定个数的整数,并放入集合中 22 int num = 5; 23 getNumList(num, () -> new Random().nextInt(100)).stream().forEach(System.out::println); 24 // Function<T, R>:函数型接口; 25 // 案例:字符串处理(转大小写、截取等) 26 System.out.println(strHandler("abcdefg", str -> str.toUpperCase())); 27 System.out.println(strHandler("abcdefg", str -> str.substring(0,3))); 28 // Predicate<T>:断言型接口(见MyFuncInterfaceDemo) 29 } 30 private static void printConsumer(String str, Consumer<String> func) { 31 func.accept(str); 32 } 33 private static String strHandler(String str, Function<String, String> func) { 34 return func.apply(str); 35 } 36 private static List<Integer> getNumList(int num, Supplier<Integer> sup) { 37 List<Integer> list = new ArrayList<>(num); 38 for (int i = 0; i < num; i++) { 39 list.add(sup.get()); 40 } 41 return list; 42 } 43} 44 45/** 46 * @desc 47 * 查询员工年龄大于25,薪资大于5000的 48 * 一、接口引用 49 * 1.常规操作 50 * 2.使用策略模式 51 * 3.使用匿名内部类 52 * 4.使用Lambda 53 * 5.使用Stream 54 * 55 * 二、方法引用 56 * 1.对象::实例方法 57 * 2.类::静态方法名 58 * 3.类::实例方法名 59 * 60 * 三、构造器引用 61 * 62 * 四、数组引用 63 * 64 * @Author xw 65 * @Date 2019/9/9 66 */ 67public class MyFuncInterfaceDemo { 68 public static void main(String[] args) { 69 70 // 一、接口引用 71 List<Employee> list = Arrays.asList( 72 new Employee("zhangsan", 20, 2500), 73 new Employee("lisi", 25, 4000), 74 new Employee("wangwu", 35, 8000), 75 new Employee("maliu", 45, 9000), 76 new Employee("tianqi", 50, 8000), 77 new Employee("smis", 26, 3000)); 78 // 1.常规操作 79 test_normal(list); 80 // 2.使用策略格式 81 test_strategy(list); 82 // 3.使用内部类 83 test_inner_class(list); 84 // 4.使用Lambda 85 test_lambda(list); 86 // 5.使用Stream 87 test_stream01(list); 88 test_stream02(list); 89 90 // 二、方法引用 91 // 1.对象::实例方法 92 test_instance_method(); 93 // 2.类::静态方法名 94 test_static_method(); 95 // 3.类::实例方法名(第一个参数x与第二个参数y,做一个运算判断) 96 test_normal_method(); 97 98 // 三、构造器引用(实例名::new) 99 test_constructor_method(); 100 101 // 四、数组引用 102 test_array_method(); 103 } 104} 105 106/** 107 * @desc Stream常规操作 108 * 1.创建Stream的几种方式 109 * 2.流转集合的几种方式 110 * 3.统计 111 * 3.1 以总数、平均值、最小值为例 112 * 3.2 归约 113 * 4.字符串拼接 114 * 5.分组、分区 115 * @Author xw 116 * @Date 2019/9/11 117 */ 118public class StreamOperatorDemo { 119 static List<Employee> employees = Arrays.asList( 120 new Employee("zhangsan", 40, 2500, Employee.EmployeeType.EMP), 121 new Employee("lisi", 25, 4000, Employee.EmployeeType.EMP), 122 new Employee("wangwu", 35, 2500, Employee.EmployeeType.EMP), 123 new Employee("maliu", 45, 9000, Employee.EmployeeType.MGR), 124 new Employee("tianqi", 50, 8000, Employee.EmployeeType.MGR), 125 new Employee("smis", 26, 3000, Employee.EmployeeType.EMP)); 126 127 public static void main(String[] args) { 128 // 1.创建Stream的几种方式 129 test_create_stream(); 130 // 2.转集合的几种方式 131 test_to_collect(); 132 // 3.统计(以总数、平均值、最小值为例) 133 test_to_count(); 134 // 4.字符串拼接 135 test_append_str(); 136 // 5.分组、分区 137 test_groupby_partitioningBy(); 138 } 139}
更多操作,请移步:https://line007.github.io/2019/10/28/jdk8%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/#more
github地址:https://github.com/line007/jucdemo2