简体   繁体   中英

Stream of Stream: cannot convert from Stream<Object>

The goal is to call a function with all elements from a stream of stream.

The issues I have is more complicated, but I manage to reproduce with this simple code.

With the following code, I get this error:

 Type mismatch: cannot convert from Stream<Object> to Stream<Integer>



    import java.util.List;
    import java.util.stream.Stream;

    class Ideone
    {
        public class A {
            String a = "a";
            List<B> bList;
        }


        public class B {
            String b = "b";
            List<String> urls;
        }


        public Stream<Integer> getResult (String a, String b, String url) {
            System.out.println("Bingo!");
            return Stream.empty();
        }



        public static void main (String[] args) throws java.lang.Exception
        {
            A a = new A();
           Stream<Integer> rez = a.bList.stream().map(b -> b.urls.stream().map(u -> getResult(a.a, b.b, u)));
        }
    }

Replace

a.bList.stream().map(...)

with

a.bList.stream().flatMap(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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