一、list转map
- (1)key:list[i]某个字段,value:list[i]
list.stream().collect(Collectors.toMap(Object::getXX,Function.identity()))
- (2)key:list[i]某个字段,value:list[i]某个字段
list.stream().collect(Collectors.toMap(Object::getXX,Object::getYY))
- (3)将list按某个字段分组
list.stream().collect(Collectors.toMap(Collectors.groupingBy(Object::getXX)))
二、list排序
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条件filter过滤
- 获取list中符合条件的数目
list.stream().filter(item->item.getXX>YY).count()
- 获取list中符合条件list
list.stream().filter(item->item.getXX>YY).collect(Collectors.toList())
四、List获取list中对象某个字段集合
list.stream().map(EmployeeRelationEntity::getId).collect(Collectors.toList())
下面根据简单实例来看看lambda的具体应用:
1、创建实体类:部门类、员工类
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; } }
2、lambda应用
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<DeptEntity> deptEntities = initDeptList(); List<EmpEntity> empEntities = initEmpList(); //获取部门列表中的所有部门ID列表 List<Integer> deptIdList = deptEntities.stream().map(DeptEntity::getDeptId).collect(Collectors.toList()); System.out.println(String.format("获取部门列表中的所有部门ID列表:deptIdList:%s",deptIdList)); //获取员工id对应员工信息的map Map<Integer, EmpEntity> 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<Integer, BigDecimal> empSaleMoneyMap = empEntities.stream().collect(Collectors.toMap(EmpEntity::getEmpId, EmpEntity::getSaleMoney)); System.out.println(String.format("获取员工id对应的员工工资的map:empSaleMoneyMap:%s",JsonUtils.objectToJson(empSaleMoneyMap))); //获取各部门员工:将员工按部门分组 Map<Integer, List<EmpEntity>> deptEmpMap = empEntities.stream().collect(Collectors.groupingBy(EmpEntity::getDeptId)); System.out.println(String.format("获取各部门员工:将员工按部门分组:deptEmpMap:%s",JsonUtils.objectToJson(deptEmpMap))); //员工按工资正序排序 List<EmpEntity> empSaleAscSortList = empEntities.stream().sorted(Comparator.comparing(EmpEntity::getSaleMoney)).collect(Collectors.toList()); System.out.println(String.format("员工按工资正序排序:empSaleAscSortList:%s",JsonUtils.objectToJson(empSaleAscSortList))); //员工按工资逆序 List<EmpEntity> 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<EmpEntity> techEmpList = empEntities.stream().filter(item -> item.getDeptId() == 1001).collect(Collectors.toList()); System.out.println(String.format("获取技术部员工信息:techEmpList:%s",JsonUtils.objectToJson(techEmpList))); } private List<EmpEntity> 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<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、测试
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、结果
开始测试----------------- 获取部门列表中的所有部门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}] 测试结束-----------------