JDK8如何写出优雅代码

只要掌握以下3点:四大函数式接口、函数式接口常用操作、Stream流式常规操作
/**
 * @desc Java四大内置函数式接口
 *  Consumer<T>:消费型接口
 *      void accept(T t);
 *  Suppler<T>:供给型接口
 *      T get();
 *  Function<T, R>:函数型接口
 *      R apply(T t);
 *  Predicate<T>:断言型接口
 *      boolean test(T t);
 * @Author xw
 * @Date 2019/9/9
 */
public class JDKFuncInterfaceDemo {
    public static void main(String[] args) {
        // Consumer<T>:消费型接口
        printConsumer("abcd", (x) -> System.out.println(x));
        // Suppler<T>:供给型接口;
        // 案例:产生指定个数的整数,并放入集合中
        int num = 5;
        getNumList(num, () -> new Random().nextInt(100)).stream().forEach(System.out::println);
        // Function<T, R>:函数型接口;
        // 案例:字符串处理(转大小写、截取等)
        System.out.println(strHandler("abcdefg", str -> str.toUpperCase()));
        System.out.println(strHandler("abcdefg", str -> str.substring(0,3)));
        // Predicate<T>:断言型接口(见MyFuncInterfaceDemo)
    }
    private static void printConsumer(String str, Consumer<String> func) {
        func.accept(str);
    }
    private static String strHandler(String str, Function<String, String> func) {
        return func.apply(str);
    }
    private static List<Integer> getNumList(int num, Supplier<Integer> sup) {
        List<Integer> list = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            list.add(sup.get());
        }
        return list;
    }
}

/**
 * @desc
 * 查询员工年龄大于25,薪资大于5000的
 * 一、接口引用
 * 1.常规操作
 * 2.使用策略模式
 * 3.使用匿名内部类
 * 4.使用Lambda
 * 5.使用Stream
 *
 * 二、方法引用
 * 1.对象::实例方法
 * 2.类::静态方法名
 * 3.类::实例方法名
 *
 * 三、构造器引用
 *
 * 四、数组引用
 *
 * @Author xw
 * @Date 2019/9/9
 */
public class MyFuncInterfaceDemo {
    public static void main(String[] args) {

        // 一、接口引用
        List<Employee> list = Arrays.asList(
                new Employee("zhangsan", 20, 2500),
                new Employee("lisi", 25, 4000),
                new Employee("wangwu", 35, 8000),
                new Employee("maliu", 45, 9000),
                new Employee("tianqi", 50, 8000),
                new Employee("smis", 26, 3000));
        // 1.常规操作
        test_normal(list);
        // 2.使用策略格式
        test_strategy(list);
        // 3.使用内部类
        test_inner_class(list);
        // 4.使用Lambda
        test_lambda(list);
        // 5.使用Stream
        test_stream01(list);
        test_stream02(list);

        // 二、方法引用
        // 1.对象::实例方法
         test_instance_method();
        // 2.类::静态方法名
         test_static_method();
        // 3.类::实例方法名(第一个参数x与第二个参数y,做一个运算判断)
         test_normal_method();

        // 三、构造器引用(实例名::new)
         test_constructor_method();

        // 四、数组引用
        test_array_method();
    }
}

/**
 * @desc Stream常规操作
 * 1.创建Stream的几种方式
 * 2.流转集合的几种方式
 * 3.统计
 *  3.1 以总数、平均值、最小值为例
 *  3.2 归约
 * 4.字符串拼接
 * 5.分组、分区
 * @Author xw
 * @Date 2019/9/11
 */
public class StreamOperatorDemo {
    static List<Employee> employees = Arrays.asList(
            new Employee("zhangsan", 40, 2500, Employee.EmployeeType.EMP),
            new Employee("lisi", 25, 4000, Employee.EmployeeType.EMP),
            new Employee("wangwu", 35, 2500, Employee.EmployeeType.EMP),
            new Employee("maliu", 45, 9000, Employee.EmployeeType.MGR),
            new Employee("tianqi", 50, 8000, Employee.EmployeeType.MGR),
            new Employee("smis", 26, 3000, Employee.EmployeeType.EMP));

    public static void main(String[] args) {
        // 1.创建Stream的几种方式
        test_create_stream();
        // 2.转集合的几种方式
        test_to_collect();
        // 3.统计(以总数、平均值、最小值为例)
        test_to_count();
        // 4.字符串拼接
        test_append_str();
        // 5.分组、分区
        test_groupby_partitioningBy();
    }
}

更多操作,请移步: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/

原文地址:https://www.cnblogs.com/ice-line/p/11753498.html