list.stream().sorted(Comparator.comparing(OrganizationVo::getOrgId).reversed()).collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(OrganizationVo::getOrgId).thenComparing(OrganizationVo::getSort))
.collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(OrganizationVo::getOrgId,Comparator.reverseOrder())).collect(Collectors.toList());
list.stream().sorted((o1, o2) -> Integer.parseInt(o2.getDeviceType()) -Integer.parseInt(o1.getDeviceType()))
.collect(Collectors.toList());
list.stream().sorted((o1, o2) -> ServiceImpl.getSort(o1).compareTo(NewsServiceImpl.getSort(o2)) <= 0 ? -1 : 1)
.collect(Collectors.toList());
public static Integer getSort(NewsDto dto) {
return Integer.parseInt(dto.getNewsId());
}
Map<String, OrganizationVo> orgMap = orgList.stream()
.collect(Collectors.toMap(OrganizationVo::getOrgId, Function.identity(), (a, b) -> a));
list.stream().map(e -> e.getList).flatMap(t-> t.stream()).collect(Collectors.toList());
List<Long> collect = list.stream()
.map(Map::keySet)
.collect(Collectors.collectingAndThen(Collectors.toList(),
t -> t.stream().flatMap(Collection::stream).collect(Collectors.toList())));
Set<Object> set = Sets.newConcurrentHashSet();
List<OrganizationVo> resultList =
list.stream().filter(vo -> set.add(vo.getOrgId())).collect(Collectors.toList());
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Set<Object> set = Sets.newConcurrentHashSet();
return t -> set.add(keyExtractor.apply(t));
}
List<OrganizationVo> resultList=
list.stream().filter(distinctByKey(vo -> vo.getOrgId())).collect(Collectors.toList());
list.stream().collect(Collectors.groupingBy(OrganizationVo::getOrgId));
Map<String, Long> collect = relatedList.stream()
.collect(Collectors.groupingBy(GroupProductRelationDto::getGroupId, Collectors.counting()));
Map<String, String> collect = detailList.stream()
.collect(Collectors.groupingBy(WorkOrderDetailDto::getOrgId,
Collectors.collectingAndThen(Collectors.toList(), t -> t.stream().map(WorkOrderDetailDto::getServiceName).collect(Collectors.joining(",")))));
Map<String, Integer> stockMap = oriSkuList.stream().collect(Collectors.groupingBy(ProductSkuDto::getProductId,
Collectors.collectingAndThen(Collectors.toList(), t -> t.stream().mapToInt(ProductSkuDto::getSkuStock).sum())));
Map<String, List<Integer>> stockListMap = oriSkuList.stream().collect(Collectors.groupingBy(ProductSkuDto::getProductId,
Collectors.collectingAndThen(Collectors.toList(), t -> t.stream().map(ProductSkuDto::getSkuStock).collect(Collectors.toList()))));
Map<String, List<String>> collect2 =
list.stream().collect(Collectors.groupingBy(GroupProductRelationDto::getBizId,
Collectors.collectingAndThen(Collectors.toList(),
t -> t.stream().map(GroupProductRelationDto::getGroupId).collect(Collectors.toList()))));
GroupProductRelationDto(groupId=0, groupIdList=null, bizId=1111, bizIdList=null)
GroupProductRelationDto(groupId=1, groupIdList=null, bizId=1111, bizIdList=null)
GroupProductRelationDto(groupId=2, groupIdList=null, bizId=1111, bizIdList=null)
GroupProductRelationDto(groupId=3, groupIdList=null, bizId=1111, bizIdList=null)
GroupProductRelationDto(groupId=5, groupIdList=null, bizId=222, bizIdList=null)
GroupProductRelationDto(groupId=6, groupIdList=null, bizId=222, bizIdList=null)
GroupProductRelationDto(groupId=7, groupIdList=null, bizId=222, bizIdList=null)
GroupProductRelationDto(groupId=8, groupIdList=null, bizId=222, bizIdList=null)
----------------------------
{"222":["5","6","7","8"],"1111":["0","1","2","3"]}
List<Integer> list= Lists.newArrayList(1,2,3);
Integer reduce = list.stream().reduce(0, (x, y) -> x = x + y);
System.out.println(reduce);
-------------------
6
List<Integer> list = Lists.newArrayList(1,2,3);
int result2 = list.stream().mapToInt(t -> t).sum();
double result3 = list.stream().mapToDouble(t -> t).sum();
long result4 = list.stream().mapToLong(t -> t).sum();
System.out.println("int计算值:" + result2);
System.out.println("double计算值:" + result3);
System.out.println("long计算值:" + result4);
-------------------
int计算值:6
double计算值:6.0
long计算值:6
List<BigDecimal> list1 = Lists.newArrayList(BigDecimal.valueOf(2.87),BigDecimal.valueOf(3.555));
BigDecimal sum1 = list1.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(sum1);
-------------------
6.425
List<BigDecimal> list2 = Lists.newArrayList(new BigDecimal("2.87"),new BigDecimal("3.555"));
BigDecimal sum2 = list2.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(sum2);
-------------------
6.425
List<BigDecimal> list3 = Lists.newArrayList(new BigDecimal(2.87),new BigDecimal(3.555));
BigDecimal sum3 = list3.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(sum3);
-------------------
6.425000000000000266453525910037569701671600341796875
tips: BigDecimal对象创建应使用 BigDecimal(String val) 构造方法
或者 BigDecimal.valueOf(double val) 静态方法来创建对象.否则就会出现示例中的精度问题.