Campbell Ritchie wrote:Please explain more. Are all the lines in the form “Gain:3:Loss:4:Processed:2:Sent:1:”?
Are you using “Gain” as the “K”? In which case you might overwrite the “V”s and your Map might only contain a mapping for the last line.
Don't declare Streams if you can possibly help it. Once you have used a Stream, it is not possible to go back to it, so it is very unusual to write Stream<XYZ> stream = ...;
What you have in line 13 might be a Stream<String> but in line 14 you are turning it into a Stream<String>, so the type will be wrong and line 13 won't compile. You will have to declare it as type Map<...>
Use an IDE to write your code; if you hover your mouse over the method names, you will see a popup telling you the exact return type of each method. Not Stream<T> or anything vague, but Stream<String>.
Let's see what you have: In line 13 you are creating a Stream<String>, but you have some duplicated code. Don't write Paths.get(...) because you have already executed the selfsame code in line 9. Write filePath.
Line 14 changes the Steam to a Stream<String> by correct use of the split() method. If you look here (Java™ Tutorials), you will find that “:” isn't a metacharacter.
You were right to start line 16 with .collect(), but that needs an argument. It is usual to pass a Collector reference; the easiest way to get such a reference being this. Note that will give you a Map<K, List<V>>. I don't know whether that is going to help in you current situation, but at least it will include every line.
Adding discussion to our Streams forrum.
Campbell Ritchie wrote:You can probably find a data structure for what you want, but please start by explaining exactly what you want your records to look like.
Don't try arrays, though.
As Campbell said, you can get a Map<String,List<Integer>) from the stream. This would work for you PROVIDING all your lines have exactly the same keys. Then the list is accessible by map.get( "Gain" ).get( lineNumber ). IF you have different keys you could use List<Map<String,Integer>>.
Campbell Ritchie wrote:You were right to start line 16 with .collect(), but that needs an argument. It is usual to pass a Collector reference; the easiest way to get such a reference being this. Note that will give you a Map<K, List<V>>. I don't know whether that is going to help in you current situation, but at least it will include every line.
Piet Souris wrote:If every line starts with "Gain:" then this will not work, since every key of your Map would be the same. What about the already suggested Map<String, List<Integer>>? That would work, but there would be only one key ("Gain") so you might just as well make a List<Integer>.
But why do you insist on creating a Map? What's wrong with creating a class "Activity" and make a List of these?