1 直接获取对象的值
this.categoryMapper.selectByIdList(ids).stream().map(Category::getName).collect(Collectors.toList());
2 游标循环处理
pageInfo.getResult().stream().map(spu -> {
// 2、把spu变为 spuBo
SpuBo spuBo = new SpuBo();
// 属性拷贝
BeanUtils.copyProperties(spu, spuBo);
// 3、查询spu的商品分类名称,要查三级分类
List<String> names = this.categoryService.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
// 将分类名称拼接后存入
spuBo.setCname(StringUtils.join(names, "/"));
// 4、查询spu的品牌名称
Brand brand = this.brandMapper.selectByPrimaryKey(spu.getBrandId());
spuBo.setBname(brand.getName());
return spuBo;
}).collect(Collectors.toList());
/**
* @author index
* @date 2020/10/27
**/
public class TestcollectingAndThen {
@Test
public void test(){
final int NUM = 14;
List<People> peopleList = new ArrayList<>(NUM);
String[] names = {"小张", "小龙", "小牛", "小猪", "小黑", "小红", "小白"};
for (int i = 0; i < 5; i++) {
//添加5个19岁的随机性别和名字的小朋友
peopleList.add(new People(19, (int) (Math.random() * 2), names[(int) (Math.random() * names.length)]));
}
for (int i = 5; i < 8; i++) {
//添加3个31岁的随机性别和名字的小朋友
peopleList.add(new People(31, (int) (Math.random() * 2), names[(int) (Math.random() * names.length)]));
}
for (int i = 8; i < NUM; i++) {
//添加6个22岁的随机性别和名字的小朋友
peopleList.add(new People(22, (int) (Math.random() * 2), names[(int) (Math.random() * names.length)]));
}
//collectingAndThen先对stream里的元素进行collecting,之后再对结果进行操作,
// 下面的结果是一个map,对map计算元素数目
System.out.println("分组数目:");
Integer groupCount = peopleList.stream().collect(
Collectors.collectingAndThen(Collectors.groupingBy(People::getName), Map::size));
System.out.println(groupCount);
System.out.println("-------------------------------------");
//按照名字分组
System.out.println("按照名字分组");
System.out.println(
peopleList.stream().collect(Collectors.groupingBy(People::getName))
);
System.out.println("-------------------------------------");
//按照名字分组(分组的结果是一个map),并统计每一个分组(map中的每一个value)中的元素数目
System.out.println("统计每一个分组(map中的每一个value)中的元素数目");
System.out.println(
peopleList.stream().collect(Collectors.groupingBy(People::getName, Collectors.counting()))
);
System.out.println("-------------------------------------");
//按照名字分组(分组的结果是一个map),并取出每一组的最大值
System.out.println("取出每一组的最大值");
System.out.println(
peopleList.stream().collect(Collectors.groupingBy(People::getName, Collectors.maxBy(new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o1.getAge() - o2.getAge();
}
})))
);
}
}
List<Integer> collectList = Stream.of(1, 2, 3, 4)
.collect(Collectors.toList());
System.out.println("collectList: " + collectList);
// 打印结果
// collectList: [1, 2, 3, 4]
Set<Integer> collectSet = Stream.of(2, 3, 3, 3)
.collect(Collectors.toSet());
System.out.println("collectSet: " + collectSet);
// 打印结果
// collectSet: [2,3]
private void loadStockInSku(List<Long> ids, List<Sku> skus) {
List<Stock> stockList = stockMapper.selectByIdList(ids);
if (CollectionUtils.isEmpty(stockList)) {
throw new LyException(ExceptionEnum.GOODS_STOCK_NOT_FOUND);
}
Map<Long, Integer> stockMap = stockList.stream().collect(Collectors.toMap(Stock::getSkuId, Stock::getStock));
skus.forEach(s -> s.setStock(stockMap.get(s.getId())));
}