简体   繁体   English

子子流中列表大小的总和

[英]Sum of list size in sub sub stream

I have the following structure: 我有以下结构:

class A {
    List<B> bs;   
}

class B {
    List<C> cs;
}

class C {
    List something.
}

I have List of A class and I have to get summ of all elements inside something list. 我有ListA类,我要进去的所有元素的SUMM something名单。 I tried to do the following: 我尝试执行以下操作:

    totalCount = as
            .stream()
            .map(a -> a.getBs()
                    .stream()
                    .mapToInt(b -> b.getSomething().size())
                    .sum());

But that doesn't compile. 但这不能编译。 Where is my mistake? 我的错误在哪里?

Compile error is: Error:(61, 21) java: incompatible types: no instance(s) of type variable(s) R exist so that java.util.stream.Stream<R> conforms to java.lang.Integer 编译错误为: Error:(61, 21) java: incompatible types: no instance(s) of type variable(s) R exist so that java.util.stream.Stream<R> conforms to java.lang.Integer

There are many ways to obtain this result, one possible way is to just flat everything and count the results: 获得此结果的方法有很多,一种可能的方法是仅对所有内容进行平整并计算结果:

A a = ..;
a.bs.stream()
  .flatMap(aa -> aa.cs.stream())
  .flatMap(bb -> bb.something.stream())
  .count();

Let this be a lesson why nested streams are a terrible idea. 让我们上一节课,为什么嵌套流是一个糟糕的主意。

You have a mapping function: 您具有映射功能:

Function<A, Integer> func = a -> a.getBs().stream()
                                          .mapToInt(b -> b.getSomething().size())
                                          .sum();

Put this in your initial stream and you get: 将其放入您的初始流中,您将获得:

totalCount = as
        .stream()
        .map(func);

Unless totalCount is a Stream<Integer> , then this won't compile. 除非totalCountStream<Integer> ,否则它将不会编译。

This is not as short at the other ones, but at least its has test code to verify it works. 这不是其他的那么短,但是至少它具有测试代码来验证它的工作原理。

public class NewMain {

public static void main(String[] args) {

    List<A> as = getAlist();

    int totalCount = as
            .stream()
            .map(a -> a.getBs())
            .collect(Collectors.summingInt(bs -> bs.stream()
                .map(b -> b.cs)
                .collect(Collectors.summingInt(cs -> cs.stream()
                        .map(c -> c.something)
                        .collect(Collectors.summingInt(s -> s.size()))))));
    System.out.println(totalCount);
}

private static List<A> getAlist() {
    List<A> all = new ArrayList<>();
    for (int k = 0; k < 10; k++) {
        A a = new A();
        for (int j = 0; j < 10; j++) {
            B b = new B();
            for (int i = 0; i < 10; i++) {
                C c = new C();
                c.something = Arrays.asList(1, 2, 3, 4);
                b.cs.add(c);
            }
            a.bs.add(b);
        }
        all.add(a);
    }
    return all;
}

static class A {

    List<B> bs = new ArrayList<>();

    private List<B> getBs() {
        return bs;
    }
}

static class B {

    List<C> cs = new ArrayList<>();
}

static class C {

    List something;

    List getSomething() {
        return something;
    }

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM