list.stream().collect(Collectors.toMap(Object::getXX,Function.identity()))
list.stream().collect(Collectors.toMap(Object::getXX,Object::getYY))
list.stream().collect(Collectors.groupingBy(Object::getXX))
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())
list.stream().filter(item->item.getXX>YY).count()
list.stream().filter(item->item.getXX>YY).collect(Collectors.toList())
list.stream().map(EmployeeRelationEntity::getId).collect(Collectors.toList())
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 com.dust.service.LambdaTest;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* @author 朝雨
* @description 员工类
* @date 2019/11/10
*/
public class EmpEntity implements Serializable {
private Integer empId;//员工id
private String empName;//员工姓名
private String sex;//性别
private String phone;//电话
private BigDecimal saleMoney;//工资
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;
}
}
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 void lambdaTest(){
List deptEntities = initDeptList();
List empEntities = initEmpList();
//获取部门列表中的所有部门ID列表
List deptIdList = deptEntities.stream().map(DeptEntity::getDeptId).collect(Collectors.toList());
System.out.println(String.format("获取部门列表中的所有部门ID列表:deptIdList:%s",deptIdList));
//获取员工id对应员工信息的map
Map empEntityMap = empEntities.stream().collect(Collectors.toMap(EmpEntity::getEmpId, Function.identity()));
System.out.println(String.format("获取员工id对应员工信息的map:empEntityMap:%s",JsonUtils.objectToJson(empEntityMap)));
//获取员工id对应的员工工资的map
Map empSaleMoneyMap = empEntities.stream().collect(Collectors.toMap(EmpEntity::getEmpId, EmpEntity::getSaleMoney));
System.out.println(String.format("获取员工id对应的员工工资的map:empSaleMoneyMap:%s",JsonUtils.objectToJson(empSaleMoneyMap)));
//获取各部门员工:将员工按部门分组
Map> deptEmpMap = empEntities.stream().collect(Collectors.groupingBy(EmpEntity::getDeptId));
System.out.println(String.format("获取各部门员工:将员工按部门分组:deptEmpMap:%s",JsonUtils.objectToJson(deptEmpMap)));
//员工按工资正序排序
List empSaleAscSortList = empEntities.stream().sorted(Comparator.comparing(EmpEntity::getSaleMoney)).collect(Collectors.toList());
System.out.println(String.format("员工按工资正序排序:empSaleAscSortList:%s",JsonUtils.objectToJson(empSaleAscSortList)));
//员工按工资逆序
List empSaleDescSortList = empEntities.stream().sorted(Comparator.comparing(EmpEntity::getSaleMoney).reversed()).collect(Collectors.toList());
System.out.println(String.format("员工按工资逆序:empSaleDescSortList:%s",JsonUtils.objectToJson(empSaleDescSortList)));
//获取技术部员工数量
long count = empEntities.stream().filter(item -> item.getDeptId() == 1001).count();
System.out.println(String.format("获取技术部员工数量:count:%s",count));
//获取技术部员工信息
List techEmpList = empEntities.stream().filter(item -> item.getDeptId() == 1001).collect(Collectors.toList());
System.out.println(String.format("获取技术部员工信息:techEmpList:%s",JsonUtils.objectToJson(techEmpList)));
}
private List initEmpList(){
EmpEntity empEntity1 = 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("4000"));
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 resultList = new ArrayList<>();
resultList.add(empEntity1);
resultList.add(empEntity3);
resultList.add(empEntity2);
return resultList;
}
//初始化部门信息
private List initDeptList(){
DeptEntity deptEntity1 = new DeptEntity();
deptEntity1.setDeptId(1001);
deptEntity1.setDeptName("技术部");
deptEntity1.setAddress("北京");
DeptEntity deptEntity2 = new DeptEntity();
deptEntity2.setDeptId(1002);
deptEntity2.setDeptName("产品部");
deptEntity2.setAddress("北京");
List result = new ArrayList<>();
result.add(deptEntity1);
result.add(deptEntity2);
return result;
}
}
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();
}
}
开始测试-----------------
获取部门列表中的所有部门ID列表:deptIdList:[1001, 1002]
获取员工id对应员工信息的map:empEntityMap:{"1":{"empId":1,"empName":"小一","sex":"M","phone":"12022020202","saleMoney":3000,"deptId":1001},"2":{"empId":2,"empName":"小二","sex":"F","phone":"12022020202","saleMoney":4000,"deptId":1001},"3":{"empId":3,"empName":"小三","sex":"M","phone":"12999999999","saleMoney":5000,"deptId":1002}}
获取员工id对应的员工工资的map:empSaleMoneyMap:{"1":3000,"2":4000,"3":5000}
获取各部门员工:将员工按部门分组: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}]
测试结束-----------------