天天看點

集合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突然就卡住了(:好尴尬,在這記錄一下。 
           

繼續閱讀