Pratim Singha
Pratim Singha

Reputation: 679

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>>.

Upvotes: 0

Views: 346

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

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);
}

Upvotes: 1

Related Questions