嗯,首先这有点误导。在你的问题中你说pick the combination having the least price from it
,但后来(和你的评论)你实际上提供了产生的样本max
result.
假设您需要最大结果,您可以使用:
long maxPrice = list
.stream()
.map(d -> Stream.concat(Stream.of(d.getDetailsId()), d.getStackableDetails().stream()))
.map(s -> s.reduce(0L, (left, right) -> left +
list.stream()
.filter(dt -> dt.getDetailsId().equals(right))
.findAny()
.get()
.getPrice()))
.max(Comparator.naturalOrder())
.orElse(0L);
System.out.println(maxPrice); // 20
EDIT
那么你想比较max price
,但输出set
做出这个价格。我唯一能想到的就是将它们放入TreeMap
,但这不是非常可读恕我直言。此外,有时您的条目会发生冲突 - 它们具有相同的最高价格。此示例仅采用遭遇顺序中的最后一个。
List<Long> highest = list
.stream()
.map(d -> Stream.concat(Stream.of(d.getDetailsId()), d.getStackableDetails().stream()).collect(Collectors.toList()))
.collect(Collectors.toMap(s -> s.stream().reduce(0L,
(left, right) -> left + list.stream().filter(dt -> dt.getDetailsId().equals(right)).findAny().get().getPrice()),
s -> s.stream().collect(Collectors.toList()),
(left, right) -> right,
TreeMap::new))
.lastEntry().getValue();
EDIT2
Map<List<Long>, Long> map = list
.stream()
.map(d -> Stream.concat(Stream.of(d.getDetailsId()), d.getStackableDetails().stream()).collect(Collectors.toList()))
.collect(Collectors.toMap(
s -> s.stream().collect(Collectors.toList()),
s -> s.stream().reduce(0L,
(left, right) -> left + list.stream().filter(dt -> dt.getDetailsId().equals(right)).findAny().get().getPrice()),
(left, right) -> right));