JAva 8 streamの一般的な操作

7180 ワード

新しい会社に行ってコードの中で大量のstream操作があることを見て、ネット上でいくつかのブログを見て勉強していくつかノートをしました
import com.google.common.collect.Lists;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Demo {
    public static List emps = Lists.newArrayList();

    static {
        emps.add(new Emp("zhangsan", 20, 1000.0, " "));
        emps.add(new Emp("lisi", 25, 2000.0, " "));
        emps.add(new Emp("wangwu", 30, 3000.0, " "));
        emps.add(new Emp("zhaoliu", 35, 4000.0, " "));
        emps.add(new Emp("liuqi", 38, 5000.0, " "));
        emps.add(new Emp("maba", 45, 9000.0, " "));
        emps.add(new Emp("houjiu", 42, 15000.0, " "));
        emps.add(new Emp("wangmazi", 55, 10000.0, " "));
    }

    public static void println(Emp emp) {
        System.out.println(String.format(" :%s, :%s, :%s, :%s", emp.getName(), emp.getAge(), emp.getSalary(), emp.getAddress()));
    }

    public static String print(Emp emp) {
        return String.format("{ :%s, :%s, :%s, :%s}", emp.getName(), emp.getAge(), emp.getSalary(), emp.getAddress());
    }

    public static void main(String[] args) {
        System.out.println("===============================================");
        // list(name)
        List listName = emps.stream().map(Emp::getName).collect(Collectors.toList());
        listName.forEach(System.out::println);
        System.out.println("===============================================");
        // set(name)
        Set setName = emps.stream().map(emp -> emp.getName()).collect(Collectors.toSet());
        setName.forEach(System.out::println);
        System.out.println("===============================================");
        // map ;(m1, m2) -> m1:map key   m1
        Map mapEmp = emps.stream().collect(Collectors.toMap(Emp::getName, Function.identity(), (m1, m2) -> m1));
        mapEmp.forEach((x, y) -> System.out.println(x + ":" + print(y)));
        System.out.println("===============================================");
        // map
        Map mapNameSalary = emps.stream().collect(Collectors.toMap(Emp::getName, Emp::getSalary, (m1, m2) -> m1));
        mapNameSalary.forEach((x, y) -> System.out.println(x + ":" + y));
        System.out.println("===============================================");
        // size   == size() 
        System.out.println(emps.stream().count());
        // 
        System.out.println(emps.stream().collect(Collectors.counting()));
        System.out.println("===============================================");
        // (salary)
        System.out.println(emps.stream().collect(Collectors.summingDouble(Emp::getSalary)));
        System.out.println("===============================================");
        // (salary)
        System.out.println(emps.stream().collect(Collectors.averagingDouble(Emp::getSalary)));
        System.out.println("===============================================");
        //   , , ,  (salary)
        DoubleSummaryStatistics salary = emps.stream().collect(Collectors.summarizingDouble(Emp::getSalary));
        System.out.println(salary.getMax());
        System.out.println(salary.getAverage());
        System.out.println(salary.getCount());
        System.out.println(salary.getMin());
        System.out.println(salary.getSum());
        System.out.println("===============================================");
        // salary - 2
        System.out.println(emps.stream().map(Emp::getSalary).collect(Collectors.maxBy(Comparator.comparing(Function.identity()))).get());
        System.out.println(emps.stream().map(Emp::getSalary).max(Comparator.comparing(Function.identity())).get());
        System.out.println(emps.stream().map(Emp::getSalary).collect(Collectors.minBy(Comparator.comparing(Function.identity()))).get());
        System.out.println(emps.stream().map(Emp::getSalary).min(Comparator.comparing(Function.identity())).get());
        System.out.println("===============================================");
        // (salary)  Emp
        println(emps.stream().max(Comparator.comparingDouble(Emp::getSalary)).get());
        println(emps.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Emp::getSalary))).get());
        System.out.println("===============================================");
        // string
        System.out.println(emps.stream().map(Emp::getName).collect(Collectors.joining()));
        System.out.println(emps.stream().map(Emp::getName).collect(Collectors.joining("|")));
        System.out.println(emps.stream().map(Emp::getName).collect(Collectors.joining(",", "[", "]")));
        System.out.println("===============================================");
        //reduce  
        Double aDouble = emps.stream().map(Emp::getSalary).reduce((a, b) -> a + b).get();
        System.out.println(aDouble);
        Double reduce = emps.stream().map(Emp::getSalary).reduce(0.0, (a, b) -> a + b, (a, b) -> 0.0);
        System.out.println(reduce);
        System.out.println("===============================================");
        // 
        Map> listMap = emps.stream().collect(Collectors.groupingBy(Emp::getAddress));
        listMap.forEach((x,y)->{
            System.out.println(x+" :");
            y.stream().forEach(Demo::println);
            System.out.println("------------------------------------------------");
        });
        Map> booleanListMap = emps.stream().collect(Collectors.groupingBy(x -> x.getSalary() > 3000.0));
        booleanListMap.forEach((x,y)->{
            System.out.println(x+" ");
            y.stream().forEach(Demo::println);
            System.out.println("------------------------------------------------");
        });
    }
}

次はビジネスクラスです
public class Emp {
    private String name;

    private Integer age;

    private Double salary;

    private String address;

    public Emp() {
    }

    public Emp(String name, Integer age, Double salary, String address) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

}