Lamda 表达式
1.分组
List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起:
Map<Integer,List<Apple>> groupBy = appleList .stream().collect(Collectors.groupingBy(Apple::getId));
//多字段分组
Map<String, List<YourBean>> mapBy= list.stream().collect(
Collectors.groupingBy(i -> i.getPriceId() + i.getId(), Collectors.toList()));
2.List转Map
Map<Integer,Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a->a, (k1,k2) ->k1));
list转map的时候有时候会将date类型作为key,实际情况中使用string的多,我们可以将某个字段转成string
Map<String, WorkCenterLoadVo> workCenterMap = list.stream().collect(Collectors.toMap(key->DateFormatUtils.format(key.getDate(), "yyyy-MM-dd"), a -> a,(k1,k2)->k1));
3.过滤Filter
List<Apple> filterList = appList.stream().filter(a->a.getName().equals("香蕉")).collect(Collectors.toList());
4.求和
将集合中的数据按照某个属性求和
//BigDecimal求和
BigDecimal totalMoney = appList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add);
(其中,若bigDecimal对象为null,可filter()过滤掉空指针.或者重写求和方法)
filter:
判断对象空
stream.filter(Objects::nonNull)
判断字段空
stream.filter(x -> x.getDateTime()!=null)
public class BigDecimalUtils {
public static BigDecimal ifNullSet0(BigDecimal in) {
if (in != null) {
return in;
}
return BigDecimal.ZERO;
}
public static BigDecimal sum(BigDecimal ...in){
BigDecimal result = BigDecimal.ZERO;
for (int i = 0; i < in.length; i++){
result = result.add(ifNullSet0(in[i]));
}
return result;
}
}
BigDecimal totalQuantity2 = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);
//基本类型求和
int sumAge = userList.stream().mapToInt(user::getAge).sum();
- list取交集
List<String> skuCodeListFour = skuCodeListOne.stream().filter(item -> skuCodeListTow.contains(item)).collect(Collectors.toList());
maintenanceNumEntityRightList = maintenanceNumEntityList.stream().filter(item -> maintenanceNumEntityErrorList.stream()
.map(e -> e.getContractNum()).collect(Collectors.toList()).contains(item.getContractNum())).collect(Collectors.toList());
- list取差集
maintenanceNumEntityRightList = maintenanceNumEntityList.stream().filter(item -> !maintenanceNumEntityErrorList.stream()
.map(e -> e.getContractNum()).collect(Collectors.toList()).contains(item.getContractNum())).collect(Collectors.toList());
- 赋值对应的id
List<MaintenanceNumEntity> list = maintenanceNumEntityErrorList.stream()
.map(maintenanceNumEntity -> maintenanceNumEntitiesList.stream()
.filter(maintenanceNumEntities -> maintenanceNumEntity
.getContractNum().equals(maintenanceNumEntities.getContractNum()))
.findFirst()
.map(maintenanceNumEntities -> {
maintenanceNumEntity.setId(maintenanceNumEntities.getId());
return maintenanceNumEntity;
}).orElse(null))
.collect(Collectors.toList());
- 最值
求最小与最大,使用min max方法
//最小
Date minEntryDate = userList.stream().map(User::getEntryDate).min(Date::compareTo).get();
//最大
Date maxEntryDate = userList.stream().map(User::getEntryDate).max(Date::compareTo).get();
9.排序
//可通过Sort对单字段多字段排序
//排序
//单字段排序,根据id排序
userList.sort(Comparator.comparing(User::getId));
//多字段排序,根据id,年龄排序
userList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));
10.去重
// 可通过distinct方法进行去重
List<Long> distinctIdList = idList.stream().distinct().collect(Collectors.toList());
//针对属性去重
List<AddOutboundNoticeDetailsBatchVo> entryDetailsBatchDistinctBatchIdList = entryDetailsBatchList.stream().filter(distinctByKey(b -> b.getMaterialBatchNumberId())).collect(Collectors.toList());
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
11.获取list某个字段组装新list
//获取list对象的某个字段组装成新list
List<Long> userIdList = userList.stream().map(a ->a.getId()).collect(Collectors.toList());
12.不同实体的list拷贝
List<TimePeriodDate> timePeriodDateList1 = calendarModelVoList.stream().map(p->{TimePeriodDate e = new TimePeriodDate(); e.setStartDate(p.getBegin());e.setEndDate(p.getEnd()); return e;}).collect(Collectors.toList());