获取集合中某个字段值的集合
List<Long> userIdList = vipOrders.stream()
.map(VipOrder::getUserId)
.collect(Collectors.toList());
获取map<T,T> 格式数据
// 格式为:{1=1号,2=2号} 后面可以返回实体类functity
// 格式为:{1=1号,2=2号} 后面可以返回实体类functity
Map<Long, String> vipSkuMap = vipSkuList.stream()
.collect(Collectors.toMap(VipSku::getId, VipSku::getSkuName));
Map<Long, VipSku> vipSkuMap = vipSkuList.stream()
.collect(Collectors.toMap(VipSku::getId, Function.identity()));
Map<String, BackcalProgramDimension> dimensionMap = programDimensionList
.stream()
.collect(Collectors.toMap(k -> k.getOrganizationId() + "|" + k.getRoleId() , v->v));
List<DropResponse> respList = list.stream()
.map(b -> new DropResponse(b.getId(), b.getBusinessName()))
.collect(Collectors.toList());
组成新的集合返回(BeanUtils.copyProperties)
List<VipOrderDetailResponse> list = vipOrders.stream().map(vipOrder -> {
VipOrderDetailResponse response = new VipOrderDetailResponse();
BeanUtils.copyProperties(vipOrder, response);
response.setCashFee(new BigDecimal(vipOrder.getCashFee()));
response.setAmount(new BigDecimal(vipOrder.getAmount()));
return response;
}).collect(Collectors.toList());
判断值是否在集合中
// 判断考核期是否存在考核期表内
// 判断考核期是否存在考核期表内
boolean isExitPeriod = periodList.stream()
.map(Period::getPeriod)
.collect(Collectors.toList()).contains(period);
过滤掉条件为false的,只保留为true的
// 最后面还可以加上 .orElse(null) 意思是:如果对象为空,则返回orElse括号里的内容。filter里面可以有多个条件使用&
Optional<CalculationConstantHistory> calculationConstantItem = calculationConstantList.stream()
.filter(item -> item.getConstantId().equals(calculationConstant.getConstantId()))
.findFirst();
// 最后面还可以加上 .orElse(null) 意思是:如果对象为空,则返回orElse括号里的内容。filter里面可以有多个条件使用&
// 判断上面对象是否存在
if (calculationConstantItem.isPresent()) {
// 业务逻辑
}
list 对象属性进行去重,并排序返回新的集合
Collectors.collectingAndThen 的第二个参数 ArrayList::new 收集起来的集合然后调用第一个参数的去重方法,然后返回新的集合。
treeSet 就是用比较器来进行排序去重,如果 compareTo 返回0,说明是重复的,返回的是自己的某个属性和另一个对象的某个属性的差值,如果是负数,则往前面排,如果是正数,往后面排,是一个循环比较的过程,一旦有一个相等,就不再比较。
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("111", 132774, 12, "1"));
studentList.add(new Student("123", 13556, 15, "1");
studentList.add(new Student("1146", 13165142, 16, "1"));
studentList.add(new Student("111", 132774, 14, "2"));
studentList.add(new Student("141321", 5641542, 15,"2"));
studentList.add(new Student("1454135", 2222542, 15, "2"));
List<Student> collect = studentList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Student::getStuName))),ArrayList::new));
System.out.println(collect);
System.out.println(collect.size()); // 输出结果是五条
list对象某属性值去重,并返回该值集合
//方案上所有去重后的组织
List<String> orgIdList = programDimensionList.stream()
.map(BackcalProgramDimension::getOrganizationId)
.distinct()
.collect((Collectors.toList());