我有一个价格和价格组的List

static class PriceGroup {
    String priceName;
    String priceGroup;
}

static class Price {
    String priceName;
    Integer price;
}


以下是一些示例数据,以及我编写的一个实现,它为每个PriceGroup找到了最低的Price。有什么建议我可以重构它以利用流来加入这些数据吗?

List<PriceGroup> priceGroups = Arrays.asList(
        new PriceGroup("F1", "Friends"),
        new PriceGroup("F2", "Friends"),
        new PriceGroup("O1", "Others"),
        new PriceGroup("O2", "Others"));

List<Price> prices = Arrays.asList(
        new Price("F1", 100),
        new Price("F2", 150),
        new Price("O1", 250),
        new Price("O2", 300));

public Map<String, Integer> getBestPrices(List<PriceGroup> priceGroups, List<Price> prices)
{
    Map<String, Integer> bestPrice = new HashMap<String, Integer>();
    for (PriceGroup priceGroup : priceGroups) {
        if (bestPrice.get(priceGroup.priceGroup) == null) {
            bestPrice.put(priceGroup.priceGroup, 10000000);
        }

        for (Price price : prices) {
            if (price.priceName.equals(priceGroup.priceName)) {
                bestPrice.put(priceGroup.priceGroup, Math.min(price.price, bestPrice.get(priceGroup.priceGroup)));
            }
        }
    }

    return bestPrice;
}


对于给定的数据,我的函数应返回一个包含以下内容的地图:


F1 => 100
O1 => 250

最佳答案

要加入2个列表,您可以考虑创建专用对象:

class Joined {
    String priceGroup;
    String priceName;
    Integer price;
    ...


然后,使用flatMap可以将priceGroups联接到prices字段上的priceName并按priceGroup分组:

Map<String, Optional<Joined>> map = priceGroups.stream()
        .flatMap(group -> prices.stream()
                .filter(p -> p.getPriceName().equals(group.getPriceName()))
                .map(p -> new Joined(group.getPriceGroup(), group.getPriceName(), p.getPrice())))
        .collect(groupingBy(Joined::getPriceGroup, minBy(Comparator.comparing(Joined::getPrice))));


现在从地图获取值,您可以打印预期结果:

for (Optional<Joined> value : map.values()) {
        value.ifPresent(joined -> System.out.println(joined.getPriceName() + " " + joined.getPrice()));
    }

// O1 250
// F1 100

09-10 21:32