分割列表

视频讲解: https://www.bilibili.com/video/av78106120/

EmployeeTestCase.java
package com.example.demo;

import lombok.extern.log4j.Log4j2;
import org.junit.Test;

import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Log4j2
public class EmployeeTestCase extends BaseTestCase {
    @Test
    public void partitioningBy(){
       Map<Boolean, List<Employee>> map = list.stream().collect(Collectors.partitioningBy(employee -> {
           return employee.getSalary() > 1500;
       }));
       log.info("true:{}",map.get(Boolean.TRUE));
       log.info("false:{}",map.get(Boolean.FALSE));
    }
}
BaseTestCase.java
package com.example.demo;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

结果:

11:03:43.159 [main] INFO com.example.demo.EmployeeTestCase - true:[Employee(id=2, name=Michael, salary=2000.0), Employee(id=5, name=Denny, salary=2000.0)]
11:03:43.162 [main] INFO com.example.demo.EmployeeTestCase - false:[Employee(id=1, name=Alex, salary=1000.0), Employee(id=3, name=Jack, salary=1500.0), Employee(id=4, name=Owen, salary=1500.0)]

 

 

版权声明:本文为JavaWeiBianCheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/JavaWeiBianCheng/p/11987957.html