lambda多个字段分组按⽤户id和渠道分组。⽰例
import org.apachemons.lang3.tuple.Pair;
public static void main(String[] args) {
List<FrequentlyUsedShopDTO> list = new ArrayList<>();
list.add(new FrequentlyUsedShopDTO().setChannel("W").setUserId(1).setFrequency(1));
list.add(new FrequentlyUsedShopDTO().setChannel("W").setUserId(1).setFrequency(11));
list.add(new FrequentlyUsedShopDTO().setChannel("W").setUserId(2).setFrequency(2));
list.add(new FrequentlyUsedShopDTO().setChannel("y").setUserId(1).setFrequency(3));
list.add(new FrequentlyUsedShopDTO().setChannel("y").setUserId(1).setFrequency(31));
list.add(new FrequentlyUsedShopDTO().setChannel("y").setUserId(2).setFrequency(4));
list.add(new FrequentlyUsedShopDTO().setChannel("y").setUserId(2).setFrequency(41));
list.add(new FrequentlyUsedShopDTO().setChannel("W").setUserId(3).setFrequency(5));
// ⽅式1
// Map<Pair<Integer, String>, List<FrequentlyUsedShopDTO>> collect = list.stream()
// .upingBy(new Function<FrequentlyUsedShopDTO, Pair<Integer, String>>() {
// @Override
// public Pair<Integer, String> apply(FrequentlyUsedShopDTO frequentlyUsedShopDTO) {
// return Pair.UserId(), Channel());
variable used in lambda// }
// }));
// ⽅式2
Map<Pair<Integer, String>, List<FrequentlyUsedShopDTO>> collect = list.stream().
upingBy(user -> Pair.UserId(), Channel()),
Collectors.mapping((FrequentlyUsedShopDTO m) -> m, toList())));
collect.forEach((key, value) -> {
System.out.Key() + " " + Value() + " " + Left() + " " + Right());
System.out.println(value);
});
打印结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论