JDK8如何写出优雅代码

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

博客地址:https://line007.github.io/

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Mybatis Plus代码方式实现多表关联查询

 Mapper接口如下:例1@Select("SELECTt\_question.\,t\_student.\name\FROMt\_question,t\_studentWHEREt\_question.student\_idt\_student.id")List<QuestionStudentVOgetQ