简体   繁体   中英

Convert List<List<String>> to Stream<Stream<String>>

Requirement: Please consider a function which generates List<List<String>> and return the same.

So in Java 1.7, this can be considered as:

public List<List<String> function(){
  List<List<String>> outList = new ArrayList<>();
  List<String> inList = new ArrayList<>();
  inList.add("ABC");
  inList.add("DEF");
  outList.add(inList);
  outList.add(inList);
  return outList;
}

Now in Java 8, the signature of the function provided to me is:

public Stream<Stream<String>> function() {
      List<List<String>> outList = new ArrayList<>();
      List<String> inList = new ArrayList<>();
      inList.add("ABC");
      inList.add("DEF");
      outList.add(inList);
      outList.add(inList);
      //How to convert the outList into Stream<Stream<String>> and return.
}

How to convert List<List<String>> into Stream<Stream<String>> .

How to convert List<List<String>> into Stream<Stream<String>> :

You need just to this :

return outList.stream()
        .map(List::stream);

Or another way without using Lists, you can use :

public Stream<Stream<String>> function() {
    Stream<String> inList = Stream.of("ABC", "DEF");
    return Stream.of(inList, inList);
}

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