I am trying to flatten my String[] stream to a String Stream
E.g.
{ "A", "B", "C" }, {"D, "E" } to "A", "B", "C", "D", "E"
This is the code I have so far:
Files.lines(Paths.get(file)).map(a -> a.split(" "));
Files.lines(path)
returns Stream[String]
and I split every string by " " to get an array of all the words ( so now Stream<String[]>
.
I would like flatten each array of words into individual elements , so Stream[String]
instead of Stream<String[]>
When i use flatMap instead of map I get an error : Type mismatch: cannot convert from String[] to Stream<? extends Object>
I thought flatMap
is used for this purpose? What's the best way to accomplish what I am trying to do
Question from professor:
Using streams : Write a method to classify words in a file according to length:
public static Map<Integer,List<String>> wordsByLength(String file)
throws IOException {
// COMPLETE THIS METHOD
}