Lambda expressions Applications

A, list turn map

- (1) key: list [i] a field, value: list [i]

 list.stream().collect(Collectors.toMap(Object::getXX,Function.identity()))

- (2) key: list [i] a field, value: list [i] a field

list.stream().collect(Collectors.toMap(Object::getXX,Object::getYY))

- (3) The list grouped by a field

list.stream().collect(Collectors.toMap(Collectors.groupingBy(Object::getXX)))

Two, list sorting

list.sort((index2,index1)->index1.getXX.compareTo(index2.getXX))
从小到大:list.stream().sorted(Comparator.comparing(Object::getXX)).collect(Collectors.toList())
从大到小:list.stream().sorted(Comparator.comparing(Object::getXX).reversed()).collect(Collectors.toList())

Three, List filter condition filter

- Get the list in the number of qualified

list.stream().filter(item->item.getXX>YY).count()

- Get the list of eligible list

list.stream().filter(item->item.getXX>YY).collect(Collectors.toList())

Four, List Gets list of objects in a field collection

list.stream().map(EmployeeRelationEntity::getId).collect(Collectors.toList())

The following simple example to look at the specific application of lambda:

1, create entity classes: class department, employee category

package com.dust.service.LambdaTest;

import java.io.Serializable;

/**
 * @author 朝雨
 * @description 部门类
 * @date 2019/11/10
 */
public class DeptEntity implements Serializable {
    private Integer deptId;//部门id
    private String deptName;//部门名称
    private String address;//部门的地址

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getAddress() {
        return address;
    }

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

  

Package Penalty for com.dust.service.LambdaTest; 

Import the java.io.Serializable;
 Import java.math.BigDecimal; 

/ ** 
 * @author Zhaoyu 
 * @description employee category 
 * @date 2019/11/10 
 * / 
public  class EmpEntity the implements {Serializable
     private Integer empId; // employees the above mentioned id 
    private String empName; // employee name 
    private String sex; // gender 
    private String phone; // telephone 
    private BigDecimal saleMoney; // wage 
    private Integer deptId;//所属部门

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public BigDecimal getSaleMoney() {
        return saleMoney;
    }

    public void setSaleMoney(BigDecimal saleMoney) {
        this.saleMoney = saleMoney;
    }
}

2, lambda application

package com.dust.service.LambdaTest;

import com.dust.util.JsonUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 朝雨
 * @description
 * @date 2019/11/10
 */
@Service
public class SearchService {

    public voidlambdaTest () { 
        List <DeptEntity> deptEntities = initDeptList (); 
        List <EmpEntity> empEntities = initEmpList ();
         // get all Sectors list ID list 
        List <Integer> = deptIdList deptEntities.stream () Map (DeptEntity. getDeptId ::) .collect (Collectors.toList ()); 
        System.out.println (String.format ( "get all sectors list ID list: deptIdList:% S" , deptIdList));
         // get employee id employee information corresponding to Map 
        the Map <Integer, EmpEntity> empEntityMap = empEntities.stream () the collect (Collectors.toMap (EmpEntity :: getEmpId, Function.identity ()));. 
        System.out.println (String.format ( "Get employee id employee information corresponding to the map: empEntityMap:%s", JsonUtils.objectToJson (empEntityMap)));
         // get id corresponding wages of employees of the Map 
        the Map <Integer, BigDecimal> = empSaleMoneyMap . EmpEntities.stream () the collect (Collectors.toMap (EmpEntity :: getEmpId, EmpEntity :: getSaleMoney )); 
        System.out.println (String.format ( "id corresponding employees get wages and salaries of the Map: empSaleMoneyMap:% S" , JsonUtils.objectToJson (empSaleMoneyMap)));
         // get each department's staff: the employees by department grouping 
        the Map <Integer, List <EmpEntity >> deptEmpMap = empEntities.stream () the collect (Collectors.groupingBy (EmpEntity :: getDeptId));. 
        System.out.println (String.format ( "get all sectors of employees: employee press sectoral clusters: deptEmpMap:% S " , (deptEmpMap))) JsonUtils.objectToJson;
         // staff sorted positive sequence wages
        List <EmpEntity> empSaleAscSortList = empEntities.stream () sorted (Comparator.comparing (EmpEntity :: getSaleMoney)) the collect (Collectors.toList ());.. 
        System.out.println (String.format ( "the wages of employees by positive sequence Sort: empSaleAscSortList:% S " , JsonUtils.objectToJson (empSaleAscSortList)));
         // employee wages by reverse 
        List <EmpEntity> = empSaleDescSortList . empEntities.stream () sorted (Comparator.comparing (EmpEntity :: getSaleMoney) .reversed () ) .collect (Collectors.toList ()); 
        System.out.println (String.format ( "the wages of employees by reverse order: empSaleDescSortList:% S" , JsonUtils.objectToJson (empSaleDescSortList)));
         // get the technology staff, the number of 
        longempEntities.stream = COUNT () filter (Item -> item.getDeptId () == 1001. ) .count (); 
        System.out.println (String.format ( "Get Number of employees Technology: COUNT:% S" , COUNT));
         // technical department employee access to information 
        List <EmpEntity> techEmpList = empEntities.stream () filter (Item -> item.getDeptId () == 1001. ) .collect (Collectors.toList ()); 
        System.out .println (String.format ( "employee information acquisition portion technology: techEmpList:% S" , JsonUtils.objectToJson (techEmpList))); 
    } 

    Private List <EmpEntity> initEmpList () { 
        EmpEntity empEntity1 = new new EmpEntity (); 
        empEntity1.setEmpId ( 1 );
        empEntity1.setEmpName ( "小一" ); 
        empEntity1.setPhone ( "12022020202" ); 
        empEntity1.setSaleMoney ( new BigDecimal ( "3000" )); 
        empEntity1.setSex ( "M" ); 
        empEntity1.setDeptId ( 1001 ); 

        EmpEntity empEntity2 = new EmpEntity (); 
        empEntity2.setEmpId ( 2 ); 
        empEntity2.setEmpName ( "小二" ); 
        empEntity2.setPhone ( "12022020202" ); 
        empEntity2.setSaleMoney ( new BigDecimal ( " 
        empEntity2.setSex ( "F" );
        empEntity2.setDeptId ( 1001 ); 

        EmpEntity empEntity3 = new EmpEntity (); 
        empEntity3.setEmpId ( 3 ); 
        empEntity3.setEmpName ( "小三" ); 
        empEntity3.setPhone ( "12999999999" ); 
        empEntity3.setSaleMoney ( new BigDecimal ( "5000" )); 
        empEntity3.setSex ( "M" ); 
        empEntity3.setDeptId ( 1002 ); 

        List <EmpEntity> resultList = new ArrayList <> (); 
        resultList.add (empEntity1);
        resultList.add(empEntity3);
        resultList.add(empEntity2);
        return resultList;
    }
    //初始化部门信息
    private List<DeptEntity> initDeptList(){
        DeptEntity deptEntity1 = new DeptEntity();
        deptEntity1.setDeptId(1001);
        deptEntity1.setDeptName("技术部");
        deptEntity1.setAddress("北京");

        DeptEntity deptEntity2 = new DeptEntity();
        deptEntity2.setDeptId(1002);
        deptEntity2.setDeptName("产品部");
        deptEntity2.setAddress("北京");
        List<DeptEntity> result = new ArrayList<>();
        result.add(deptEntity1);
        result.add(deptEntity2);
        return result;
    }
}

3, the test

package com.dust.test;

import com.dust.service.LambdaTest.SearchService;
import com.dust.service.proxyTest.BaseService;
import com.dust.service.proxyTest.Person;
import com.dust.service.proxyTest.ProxyFactory;
import org.checkerframework.checker.units.qual.A;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 朝雨
 * @description
 * @date 2019/11/11
 */
public class LambdaTest extends TestApplication {

    @Autowired
    private SearchService searchService;

    @Test
    public void test(){
        searchService.lambdaTest();
    }
}

4 results

Began testing ----------------- 
get all Sectors list ID list: deptIdList: [ 1001, 1002 ] 
to obtain the corresponding employee id employee information in the Map: empEntityMap: { "1 ": {" empId ": 1 ," empName ":" small a "," sex ":" M "," phone ":" 12022020202 "," saleMoney ": 3000," deptId ": 1001}," 2 " : { "empId": 2, "empName": " small two", "sex": "F ", "phone": "12022020202", "saleMoney": 4000, "deptId": 1001}, "3": { "empId": 3, " empName": " Xiao San", "Sex": "M", "Phone": "12,999,999,999", "saleMoney": 5000, "deptId": 1002 }} 
acquired employee id corresponding to employees wages Map: empSaleMoneyMap: { ". 1": 3000, "2": 4000, ". 3":5000 } 
obtain the various departments of employees: by sector employee groups: deptEmpMap: {"1001":[{"empId":1,"empName":"小一","sex":"M","phone":"12022020202","saleMoney":3000,"deptId":1001},{"empId":2,"empName":"小二","sex":"F","phone":"12022020202","saleMoney":4000,"deptId":1001}],"1002":[{"empId":3,"empName":"小三","sex":"M","phone":"12999999999","saleMoney":5000,"deptId":1002}]}
员工按工资正序排序:empSaleAscSortList:[{"empId":1,"empName":"小一","sex":"M","phone":"12022020202","saleMoney":3000,"deptId":1001},{"empId":2,"empName":"小二","sex":"F","phone":"12022020202","saleMoney":4000,"deptId":1001},{"empId":3,"empName":"小三","sex":"M","phone":"12999999999","saleMoney":5000,"deptId":1002}]
员工按工资逆序:empSaleDescSortList:[{"empId":3,"empName":"小三","sex":"M","phone":"12999999999","saleMoney":5000,"deptId":1002},{"empId":2,"empName":"小二","sex":"F","phone":"12022020202","saleMoney":4000,"deptId":1001},{"empId":1,"empName":"小一","sex":"M","phone":"12022020202","saleMoney":3000,"deptId":1001}]
获取技术部员工数量:count:2
获取技术部员工信息:techEmpList:[{"empId":1,"empName":"小一","sex":"M","phone":"12022020202","saleMoney":3000,"deptId":1001},{"empId":2,"empName":"小二","sex":"F","phone":"12022020202","saleMoney":4000,"deptId":1001}]
测试结束-----------------

 

Guess you like

Origin www.cnblogs.com/zhaoyu-dust/p/11834143.html
Recommended