分割列表java
視頻講解: https://www.bilibili.com/video/av78106120/
EmployeeTestCase.javaide
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(){ //把工資大於1500放在true的列表裏,小於或等於1500放在false列表裏 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)); }
結果:spa
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)]
關注公衆號,堅持天天3分鐘學習
3d