Java 8 stream

/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.ActiveProfiles;

import com.jeesite.common.collect.ListUtils;

/**
 * JeeSite Web
 * 
 * @author ThinkGem1234
 * @version 2018-1-8
 */
@ActiveProfiles("test")
@SpringBootApplication
public class ApplicationTest {
    public static void main(String[] args) {
        /*
         * // List<String> 去重 List<String> list = new ArrayList<String>();
         * list.add("123"); list.add("123"); list.add("123"); list.add("123");
         * System.err.println(list.size()); Set<String> setList = new
         * HashSet<String>(list); System.err.println(setList.size());
         */

        List<String> list = new ArrayList<String>();
        // 顺序流
        Stream<String> stream = list.stream();
        // 并行流
        Stream<String> parallelStream = list.parallelStream();

        Integer[] nums = new Integer[10];
        // 数组流
        Stream<Integer> stream2 = Arrays.stream(nums);

        Stream<Integer> of = Stream.of(1, 2, 3, 4, 5);
        // of.forEach(System.err::println);

        Stream<Integer> limit = Stream.iterate(1, (x) -> x * 10).limit(6);
        // limit.forEach(System.err::println);

        Stream<Double> limit2 = Stream.generate(Math::random).limit(2);
        // limit2.forEach(System.err::println);

        /*
         * try { BufferedReader reader =new BufferedReader(new
         * FileReader("D:\辅助核算项.txt"));
         * 
         * Stream<String> lineStream = reader.lines();
         * 
         * lineStream.forEach(System.out::println); } catch
         * (FileNotFoundException e) { // TODO Auto-generated catch block
         * e.printStackTrace(); }
         */

        Pattern pattern = Pattern.compile(",");
        Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
        // stringStream.forEach(System.err::println);

        Stream<Integer> stream1 = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
        Stream<Integer> newStream = stream1.filter(s -> s > 5)// 6 6 7 9 8 10 12
                                                                // 14 14
                .distinct()// 6 7 9 8 10 12 14
                .skip(2)// 9 8 10 12 14
                .limit(2);// 9 8
        // newStream.forEach(System.err::println);

        List<String> lists = Arrays.asList("a,b,c", "1,2,3");

        Stream<String> s1 = lists.stream().map(s -> s.replaceAll(",", ""));

        // s1.forEach(System.err::println);

        Stream<String> s3 = lists.stream().flatMap(s -> {
            String[] split = s.split(",");
            Stream<String> s2 = Arrays.stream(split);
            return s2;
        });
        // s3.forEach(System.err::println);

        List<String> listss = Arrays.asList("aa", "ff", "dd");

        // listss.stream().sorted().forEach(System.err::println);

        List<Integer> listsss = Arrays.asList(1, 2, 5, 4, 3, 8, 7, 6);

        // listsss.stream().sorted().forEach(System.err::println);

        Student ss1 = new Student("aa", 10, null);
        Student ss2 = new Student("bb", 20, null);
        Student ss3 = new Student("aa", 30, null);
        Student ss4 = new Student("dd", 40, null);

        List<Student> studentList = Arrays.asList(ss1, ss2, ss3, ss4);

        // studentList.stream().sorted(
        // (o1,o2) ->{
        // if(o1.getName().equals(o2.getName())){
        // return o1.getAge() - o2.getAge();
        // } else {
        // return o1.getName().compareTo(o2.getName());
        // }
        // }
        // ).forEach(System.err::println);
        // System.err.println("--------------");
        List<Student> studentList1 = Arrays.asList(ss1, ss2);
        // studentList1.stream().peek(o ->
        // o.setAge(100)).forEach(System.err::print);

        List<Integer> list31 = Arrays.asList(1, 2, 3, 4, 5);
        boolean allMath = list31.stream().allMatch(e -> e > 10);
        boolean noneMath = list31.stream().noneMatch(e -> e > 10);
        boolean anyMath = list31.stream().anyMatch(e -> e > 4);
        /*
         * System.err.println(allMath); System.err.println(noneMath);
         * System.err.println(anyMath);
         */

        /*
         * System.err.println(list31.stream().findFirst().get());
         * System.err.println(list31.stream().findAny().get());
         * System.err.println(list31.stream().count());
         * System.err.println(list31.stream().max(Integer::compareTo).get());
         * System.err.println(list31.stream().min(Integer::compareTo).get());
         */

        List<Integer> list32 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
                22, 23, 24);
        Integer v = list32.stream().reduce((x1,x2) -> x1 + x2).get();
        System.err.println(v);
        
        Integer v1 = list32.stream().reduce(10, (x1, x2) -> x1 + x2);
        // System.err.println(v1);  //310
        System.err.println("--------------------------");
        Integer v3 = list32.parallelStream().reduce(0, 
                (x1, x2) -> {
                    // System.err.println("parallelStream accumulator: x1:" + x1 + "  x2:" + x2);
                    return x1 - x2;
                },
                (x1, x2) -> {
                    // System.err.println("parallelStream combiner: x1:" + x1 + "  x2:" + x2);
                    return x1 * x2;
                });
        // System.err.println(v3);
        
        
        Student student1 = new Student("aa", 10, 1);
        Student student2 = new Student("bb", 20, 2);
        Student student3 = new Student("cc", 10, 3);
        
        List<Student> list331 = Arrays.asList(student1,student2,student3);
        
        List<Integer> ageList  = list331.stream().map(Student::getAge).collect(Collectors.toList());
        
        Set<Integer> ageSet = list331.stream().map(Student::getAge).collect(Collectors.toSet());
        
        Map<String, Integer> studentMap = list331.stream().collect(Collectors.toMap(Student::getName, Student::getAge));
        
        String joinName = list331.stream().map(Student::getName).collect(Collectors.joining(",", "(",")"));
    
        System.err.println(ageList);
        
        System.err.println(ageSet);
        
        System.err.println(studentMap);
        
        System.err.println(joinName);
        
        // 学生总数
        Long count = list331.stream().collect(Collectors.counting());
        
        // 最大年龄
        Integer maxAge = list331.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get();
        
        // 所有人的年龄
        Integer sumAge = list331.stream().collect(Collectors.summingInt(Student::getAge));
        
        // 平均年龄
        Double avageAge = list331.stream().collect(Collectors.averagingDouble(Student::getAge));
        
        System.err.println(count);
        System.err.println(maxAge);
        System.err.println(sumAge);
        System.err.println(avageAge);
        
        // 带上以上所有的方法
        DoubleSummaryStatistics statistics = list331.stream().collect(Collectors.summarizingDouble(Student::getAge));

        System.err.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
    
        // 分组
        Map<Integer, List<Student>> ageMap = list331.stream().collect(Collectors.groupingBy(Student::getAge));
        
        Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list331.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
        
        System.err.println(ageMap);
        
        System.err.println(typeAgeMap);
        
        // 分区
        // 分成两部分,一部分大于10岁,一部分小于等于10岁
        Map<Boolean, List<Student>> partMap = list331.stream().collect(Collectors.partitioningBy(ve -> ve.getAge() > 10));
        System.err.println(partMap);
        Integer allAge =list331.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get();
        System.err.println(allAge);
    }

}

https://blog.csdn.net/y_k_y/article/details/84633001

原文地址:https://www.cnblogs.com/shuaihan/p/14066636.html