英文:
Convert List<List<String>> to Stream<Stream<String>>
问题
Requirement:
请考虑一个生成 `List<List<String>>` 并返回相同的函数。
所以在 Java 1.7 中,可以考虑如下:
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;
}
现在在 Java 8 中,我提供的函数签名是:
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);
//如何将 outList 转换为 Stream<Stream<String>> 并返回。
}
如何将 `List<List<String>>` 转换为 `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>>
.
答案1
得分: 1
> 如何将 List<List<String>>
转换为 Stream<Stream<String>>
:
你只需要这样做:
return outList.stream()
.map(List::stream);
或者另一种方式,不使用列表,你可以这样做:
public Stream<Stream<String>> function() {
Stream<String> inList = Stream.of("ABC", "DEF");
return Stream.of(inList, inList);
}
英文:
> 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论