栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

6.java8流的使用 stream的归约与收集

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

6.java8流的使用 stream的归约与收集

1.关键代码

代码类Stream12SpecificationsAndCollect

import com.netease.streamlearningbyjava.bean.Employee2;

import org.junit.Test;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;



public class Stream12SpecificationsAndCollect {
    List employee2s = Arrays.asList(
            new Employee2("张三", 18, 9999.99, Employee2.Status.FREE),
            new Employee2("李四", 58, 5555.55, Employee2.Status.BUSY),
            new Employee2("王五", 26, 3333.33, Employee2.Status.VOCATION),
            new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
            new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
            new Employee2("田七", 12, 8888.88, Employee2.Status.BUSY)
    );
    
    @Test
    public void test3(){
        List list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream()
                .reduce(0, (x, y) -> x + y);//这个0是起始值,第一个值,y是上面的1,2,3,4,5,6,7,8,9,10,每次都加一次,加了10次
        System.out.println(sum);//55

        System.out.println("----------------------------------");
        Optional optional = employee2s.stream()
                .map(Employee2::getSalary)
                .reduce(Double::sum);
        System.out.println(optional);
    }
    
    @Test
    public void test4(){
        //提取老的list里面的值,变成一个新的list(只有name),然后打印
        List list = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toList());
        list.forEach(System.out::println);
        //结果:
        //张三
        //李四
        //王五
        //赵六
        //赵六
        //田七
        //去重
        System.out.println("--------------------------------");
        Set set = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toSet());
        set.forEach(System.out::println);
        //结果:
        //李四
        //张三
        //王五
        //赵六
        //田七
        
        HashSet hashSet = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toCollection(HashSet::new));
        hashSet.forEach(System.out::println);


    }

    @Test
    public void test5(){
        
        Long count = employee2s.stream()
                .collect(Collectors.counting());
        System.out.println(count);
        
        Double avg = employee2s.stream()
                .collect(Collectors.averagingDouble(Employee2::getSalary));
        System.out.println(avg);
        
        DoubleSummaryStatistics sum = employee2s.stream()
                .collect(Collectors.summarizingDouble(Employee2::getSalary));
        System.out.println(sum);
        
        Optional max = employee2s.stream()
                .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
        System.out.println(max);
        
        Optional min = employee2s.stream()
                .map(Employee2::getSalary)
                .collect(Collectors.minBy(Double::compare));
        System.out.println(min.get());
        
        Map> group = employee2s.stream()
                .collect(Collectors.groupingBy(Employee2::getStatus));
        System.out.println(group);
    }

}

bean类Employee2

import java.util.Objects;



public class Employee2 {
    private String name;
    private Integer age;
    private Double salary;
    private Status Status;

    public Employee2(String name, Integer age, Double salary, Employee2.Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        Status = status;
    }

    
    public enum Status{
        FREE,
        BUSY,
        VOCATION;
    }

    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;
    }

    public Employee2.Status getStatus() {
        return Status;
    }

    public void setStatus(Employee2.Status status) {
        Status = status;
    }

    @Override
    public String toString() {
        return "Employee2{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", salary=" + salary +
                ", Status=" + Status +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee2 employee2 = (Employee2) o;
        return Objects.equals(name, employee2.name) &&
                Objects.equals(age, employee2.age) &&
                Objects.equals(salary, employee2.salary) &&
                Status == employee2.Status;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary, Status);
    }


}

2.步骤与讲解 1.reduce收汁浓缩,这里起到一个合并值得左右,既1+2+3……+10;
List list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream()
                .reduce(0, (x, y) -> x + y);//这个0是起始值,x是第一个值(计算后的值),y是上面的1,2,3,4,5,6,7,8,9,10,每次都加一次,加了10次
        System.out.println(sum);//55

//下面map表示要计算某个值,这里是Salary

System.out.println("----------------------------------");
        Optional optional = employee2s.stream()
                .map(Employee2::getSalary)
                .reduce(Double::sum);
        System.out.println(optional);
2.收集 1.获取list里面的值,然后提取成一个新的list

提取老的list里面的值,变成一个新的list(只有name),然后打印

//提取老的list里面的值,变成一个新的list(只有name),然后打印
        List list = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toList());
        list.forEach(System.out::println);
        //结果:
        //张三
        //李四
        //王五
        //赵六
        //赵六
        //田七
2.去重

set是一个不包含重复元素的 collection。确切地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素

System.out.println("--------------------------------");
        Set set = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toSet());
        set.forEach(System.out::println);
        //结果:
        //李四
        //张三
        //王五
        //赵六
        //田七
3.总数,平均值,总和,最大值,最小值
        Long count = employee2s.stream()
                .collect(Collectors.counting());
        System.out.println(count);
        
        Double avg = employee2s.stream()
                .collect(Collectors.averagingDouble(Employee2::getSalary));
        System.out.println(avg);
        
        DoubleSummaryStatistics sum = employee2s.stream()
                .collect(Collectors.summarizingDouble(Employee2::getSalary));
        System.out.println(sum);
        
        Optional max = employee2s.stream()
                .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
        System.out.println(max);
        
        Optional min = employee2s.stream()
                .map(Employee2::getSalary)
                .collect(Collectors.minBy(Double::compare));
        System.out.println(min.get());
4.分組groupingBy
        Map> group = employee2s.stream()
                .collect(Collectors.groupingBy(Employee2::getStatus));
        System.out.println(group);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446064.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号