如何组合列表元素并找到最大组合的价格 [英] How to combine list elements and find the price of largest combination

查看:147
本文介绍了如何组合列表元素并找到最大组合的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中包含特定项目的详细信息,如下所示:

I have a class that holds details of a particular item like the following:

Detail.class

Long detailsId;
Integer price;
List<Long> stackableDetails;

/*getters and setters*/

现在,我有一个样本数据集如下:

Now, I have a sample dataset like the following:

    DetailId    Price    StackableDetails
------------------------------------------
    1011        4$       1012, 1014
    1012        6$       1011,1013
    1013        10$      1012
    1014        8$       1011

此数据集映射到List sampleDetails 。
现在,基于stackableDetails信息,我必须结合细节并选择具有最大价格的组合。

This data set maps to List sampleDetails. Now, based on the stackableDetails information, I have to combine the details and pick the combination having the max price from it.

For eg,
In the data set available, the possible combinations would be
1011,1012,1014 - 4+6+8 = 18$
1012,1011,1013 - 6+4+10 = 20$
1013,1012 - 10+6 = 16$
1014,1011 - 8+4 = 12$

现在细节1012,1011,1013的组合产生20 $,所以我获取这个组合并将其添加到我的结果列表中。我怎样才能在java8中实现这一点。

Now the combination of details 1012,1011,1013 yields 20$, so I fetch this combination and add this in my result list. How can I achieve this in java8.

任何帮助表示赞赏。谢谢!

Any help appreciated. Thanks!

推荐答案

嗯,首先它有点误导。在您的问题中,您说选择价格最低的组合,但稍后(和您的评论)实际上提供了产生的样本最大结果。

Well, first it's a bit misleading. In your question you say pick the combination having the least price from it, but then later (and your comments) you actually provide the samples that yields the max result.

假设您需要最大结果,您可以使用:

Assuming you need the max result, you could use this:

 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

编辑

您希望通过最高价格进行比较,但输出设置即可生成此价格。我唯一能想到的就是将它们放入 TreeMap ,但这不是非常易读恕我直言。此外,如果您有条目冲突,则会出现这种情况 - 它们具有相同的最高价格。这个例子只是采用遭遇顺序中的最后一个。

Well you want to compare by max price, but output set the make this price. The only thing I could come up with is to put them into a TreeMap, but that is not very readable IMHO. Also, there is the case when you have entries that collide - they have the same max price. This example simply takes the last one in encounter order.

  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));

这篇关于如何组合列表元素并找到最大组合的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆