我试图将我的String []流展平为String Stream
例如
{ "A", "B", "C" }, {"D, "E" } to "A", "B", "C", "D", "E"
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码:
Files.lines(Paths.get(file)).map(a -> a.split(" "));
Run Code Online (Sandbox Code Playgroud)
Files.lines(path)返回Stream[String]并用""分隔每个字符串以得到所有单词的数组(现在这样Stream<String[]>.
我想将每个单词数组拼成单个元素,Stream[String]而不是Stream<String[]>
当我使用flatMap而不是map时,我收到一个错误: Type mismatch: cannot convert from String[] to Stream<? extends Object>
我以为flatMap是用于此目的?什么是完成我想要做的最好的方法
教授提问:
使用流:编写一个方法,根据长度对文件中的单词进行分类:
public static Map<Integer,List<String>> wordsByLength(String file)
throws IOException {
// COMPLETE THIS METHOD
}
Run Code Online (Sandbox Code Playgroud)