Stream Series (xiii) GroupingBy method

Packet

Video explain  https://www.bilibili.com/video/av78225682/

 

EmployeeTestCase.java
package com.example.demo;

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

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

@Log4j2
public class EmployeeTestCase extends BaseTestCase {
    @Test
    public  void groudBy () {
         // Compensation obtain a list of employees according to 
        the Map <Double, List <= the Employee >> the Map list.stream ()
                .collect(Collectors.groupingBy(Employee::getSalary));
        System.out.println ( "the Map:" + the Map);
         // get the number of employees based on salary 
        the Map <Double, Long> MAP2 = list.stream ()
                .collect(Collectors.groupingBy(Employee::getSalary,Collectors.counting()));
        System.out.println ( "MAP2:" + MAP2);
         // get the total number of employees based on salary compensation 
        the Map <Double, Double> MAP3 = list.stream ()
                .collect(Collectors.groupingBy(Employee::getSalary,Collectors.summingDouble(Employee::getSalary)));
        System.out.println("map3:"+map3);
    }
}
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));
}

result:

map:{1500.0=[Employee(id=3, name=Jack, salary=1500.0), Employee(id=4, name=Owen, salary=1500.0)], 2000.0=[Employee(id=2, name=Michael, salary=2000.0), Employee(id=5, name=Denny, salary=2000.0)], 1000.0=[Employee(id=1, name=Alex, salary=1000.0)]}
map2:{1500.0=2, 2000.0=2, 1000.0=1}
map3:{1500.0=3000.0, 2000.0=4000.0, 1000.0=1000.0}

 No public concern, adhere to three minutes a day learning videos

 

 

Guess you like

Origin www.cnblogs.com/JavaWeiBianCheng/p/11994588.html