如何使用Java 8 Stream扩展并重新组合列表列表?

时间:2015-08-31 23:52:35

标签: java lambda java-8 java-stream

我有一个A类列表,其中包含一个List本身。

public class A {
    public double val;
    public String id;
    public List<String> names = new ArrayList<String>();
    public A(double v, String ID, String name)
    {
        val = v;
        id = ID;
        names.add(name);
    }

static public List<A> createAnExample()
{
    List<A> items  = new ArrayList<A>();

    items.add(new A(8.0,"x1","y11"));
    items.add(new A(12.0, "x2", "y21"));
    items.add(new A(24.0,"x3","y31"));
    items.get(0).names.add("y12");
    items.get(1).names.add("y11");
    items.get(1).names.add("y31");
    items.get(2).names.add("y11");
    items.get(2).names.add("y32");
    items.get(2).names.add("y33");
    return  items;
}

目的是对List上的每个id的平均val求和。我使用一些Java 8流在Main函数中添加了代码。 我的问题是如何在不使用第二个数组和for循环的情况下以更优雅的方式重写它。

static public void main(String[] args) {
    List<A> items = createAnExample();

    List<A> items2 = new ArrayList<A>();
    for (int i = 0; i < items.size(); i++) {
        List<String> names = items.get(i).names;
        double v = items.get(i).val / names.size();
        String itemid = items.get(i).id;
        for (String n : names) {
            A item = new A(v, itemid, n);
            items2.add(item);
        }
    }
    Map<String, Double> x = items2.stream().collect(Collectors.groupingBy(item ->
            item.names.isEmpty() ? "NULL" : item.names.get(0), Collectors.summingDouble(item -> item.val)));
    for (Map.Entry entry : x.entrySet())
        System.out.println(entry.getKey() + " --> " + entry.getValue());
}

2 个答案:

答案 0 :(得分:4)

您可以使用flatMap

执行此操作
x = items.stream()
    .flatMap(a -> a.names.stream()
        .map(n -> new AbstractMap.SimpleEntry<>(n, a.val / a.names.size()))
    ).collect(groupingBy(
        Map.Entry::getKey, summingDouble(Map.Entry::getValue)
    ));

如果您发现自己经常处理这类问题,请考虑使用静态方法创建Map.Entry

static<K,V> Map.Entry<K,V> entry(K k, V v) {
    return new AbstractMap.SimpleImmutableEntry<>(k,v);
}

然后你会有一个不那么冗长的.map(n -> entry(n, a.val/a.names.size()))

答案 1 :(得分:1)

在扩展标准Stream API的免费StreamEx库中,有一些特殊操作可以帮助构建这样复杂的地图。使用StreamEx可以解决您的问题:

Map<String, Double> x = StreamEx.of(createAnExample())
    .mapToEntry(item -> item.names, item -> item.val / item.names.size())
    .flatMapKeys(List::stream)
    .grouping(Collectors.summingDouble(v -> v));

这里mapToEntry创建了一个映射条目流(所谓的EntryStream),其中键是名称列表,值是平均值。接下来,我们使用flatMapKeys展平键,保持原样(因此我们有Entry<String, Double>的流)。最后,我们将它们组合在一起,对重复键的值进行求和。

相关问题