天天看点

集合List<?>根据属性分组列表

2020-08-12 18:40 小雨🌧

好久不写博客,我回来了,2020经历了太多,书不尽言,言不尽意,一切恍如在昨日。记录一下某次在实操挂掉的小点假使一个list列表:根据某个值不同,区分为不同的列表集合。采用stream写法如下: 分组

@Override
@Transactional(rollbackFor = Exception.class)
public List<?> productRecommendClass() {
    List<ProductRecommend> productRecommendList = productRecommendMapper.selectByExample(new ProductRecommendCriteria());

    // 根据位置进行数据分组,这里的groupingBy进行分组字段
    Map<Integer, List<ProductRecommend>> classifiedCollect =
            productRecommendList.stream().collect(Collectors.groupingBy(ProductRecommend::getSortPosition));

    // Map最终匹配,
    List<Map<Integer, List>> groupMapList = new ArrayList<>();
    classifiedCollect.keySet().forEach(key -> {
        Map<Integer, List> newMap = new HashMap<>();
        newMap.put(key, classifiedCollect.get(key));
        groupMapList.add(newMap);
    });
    System.out.println(groupMapList.toString());

    // List最终匹配
    List<List> groupList = new ArrayList<>();
    classifiedCollect.keySet().forEach(key -> {
        List<ProductRecommend> recommends = classifiedCollect.get(key);
        groupList.add(recommends);
    });
    System.out.println(groupList.toString());

    return groupList;
}

2、获取Map类型的集合或者List类型的集合。


电脑实操时naozi突然就卡住了(:好尴尬,在这记录一下。 
           

继续阅读