java8,stream笔记2

List emps = Arrays.asList(
            new Employee(102, "李四", 79, 6666.66, Employee.Status.BUSY),
            new Employee(101, "张三", 18, 9999.99, Employee.Status.FREE),
            new Employee(103, "王五", 28, 3333.33, Employee.Status.VOCATION),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.BUSY),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
            new Employee(105, "田七", 38, 5555.55, Employee.Status.BUSY)
    );

    //3. 终止操作
	/*
		归约
		reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。
	 */
    @Test
    public void test1() {
        List lists = Arrays.asList(1, 2, 34, 5, 86, 34, 68, 6, 78);
        Integer sum1 = lists.stream().reduce(0, (x, y) -> x + y);
        System.out.println(sum1);
        Optional sum2 = lists.stream().reduce((x, y) -> x + y);
        System.out.println(sum2.get());

        //-----------------------------------------------

        Optional reduce = emps.stream().map(e -> e.getSalary()).reduce((x, y) -> x + y);
        System.out.println(reduce.get());
    }

    //搜索名字中出现六的次数
    @Test
    public void test23() {

        emps.stream().map(e -> e.getName()).forEach(System.out::println);


        Optional op2 = emps.stream().map(e -> e.getName()).flatMap(x -> TestStream4.filterCharacter(x)).map(ch -> {
            if (ch.equals('六')) {
                return 1;
            } else {
                return 0;
            }
        }).reduce(Integer::sum);

        System.out.println(op2.get());

        System.out.println("-----");
        emps.stream().map(e -> e.getName()).map(str -> {
            if (str.contains("六")) {
                return 1;
            } else {
                return 0;
            }
        }).forEach(System.out::println);
        //用reduce 可以求和

//        System.out.println(sum2.get());
    }

    @Test
    public void testFlatp() {
        emps.stream()
                .map((e) -> e.getName()).forEach(System.out::println);

        System.out.println("-------------------------------------------");

        List strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
        strList.stream().map(x -> x.toUpperCase()).forEach(System.out::println);

        System.out.println("-----------------");

        strList.stream().map(x -> filterCharacter(x)).forEach(sm -> sm.forEach(System.out::println));

    }

    public static Stream filterCharacter(String str) {
        List list = new ArrayList<>();

        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }

        return list.stream();
    }


//    	//collect——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法

    @Test
    public void test3() {
        List list = emps.stream().map(Employee::getName).collect(Collectors.toList());
        list.forEach(System.out::println);

        System.out.println("-------------------------------------------");

        Set set = emps.stream().map(Employee::getName).collect(Collectors.toSet());
        set.forEach(System.out::println);

        System.out.println("-------------------------------------------");

        HashSet collect = emps.stream().map(Employee::getName).collect(Collectors.toCollection(() -> new HashSet<>()));
        collect.forEach(System.out::println);
    }

    @Test
    public void test4(){

        //总数
        long count = emps.stream().count();
        System.out.println(count);

        //也是求总数
        Long count2 = emps.stream().collect(Collectors.counting());
        System.out.println(count2);

        //工资平均值
        Double s1 = emps.stream().map(Employee::getSalary).collect(Collectors.averagingDouble(x -> x));
        System.out.println(s1);
        // 工资平均值2
        Double s2 = emps.stream().collect(Collectors.averagingDouble(e -> e.getSalary()));
        System.out.println(s2);

        //工资总和
        DoubleSummaryStatistics collect = emps.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println("总和:"+collect.getSum());
        System.out.println("平均工资:"+collect.getAverage());
        System.out.println(collect.getMax());
        System.out.println(collect.getCount());
        System.out.println(collect.getMin());

        //工资共和2
        Double salarySum = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println("工资总和:"+salarySum);

    }

    /**
     * 分组
     */
    @Test
    public void test6(){

        Map> collect = emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
        for (Employee.Status status : collect.keySet()) {
            System.out.println(collect.get(status));
            System.out.println("--------------");
        }

        System.out.println("---------------------");

        Map>> collect1 = emps.stream().collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy(
                (x) -> {
                    if (((Employee) x).getAge() <= 35) {
                        return "青年";
                    } else {
                        return "中年";
                    }
                }
        )));
        System.out.println(collect1);
    }

    /**
     * 分区,分片
     */
    @Test
    public void test7(){
        Map> collect = emps.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000.0));

        System.out.println(collect);
    }

    /**
     * 连接操作, join
     *
     */
    @Test
    public void test8(){
        String collect = emps.stream().map(Employee::getName).collect(Collectors.joining(","));
        System.out.println(collect);//李四,张三,王五,赵六,赵六,赵六,田七

        String collect2 = emps.stream().map(Employee::getName).collect(Collectors.joining(",","前缀-","-后缀"));
        System.out.println(collect2);//前缀-李四,张三,王五,赵六,赵六,赵六,田七-后缀
    }
}

 

你可能感兴趣的:(java8,stream笔记2)