I believe it's easier for me to just write out what I have to do step by step and copy the code I have so far instead of trying to explain it in a long text.
This is what I have to do:
- Read file line by line
- Map keys in these lines
- Merge lines by 1 key(getUri in the code below) and (get average of another key (getRequestDuration))
- Order by the avg of merged getRequestDuration (highest first)
- Return n amount of highest.
And this is what I got right now:
try(Stream<String> logs = Files.lines(Paths.get(args))) { logs.map(LogLine::parseLine).limit(10).sorted((e1, e2) -> Integer.compare(e1.getRequestDuration(), e2.getRequestDuration())) .collect(Collectors.groupingBy( LogLine::getUri, Collectors.averagingDouble(LogLine::getRequestDuration))); return logs; I'm new to Java so I have a couple of questions I hope someone can answer for me:
- How do you sort after collecting/group before sorting?
- How to limit properly? So that it limits the amount of entries I return instead of how many I go through.
I (think I) understand how streams work but I'm having troubles putting that to real world use. I'm not necessarily looking for the full code for the problem I'm facing, rather than someone who could provide me the information needed to understand how to do it. I would very much appreciate if someone wrote out how to tackle such problems with streams the proper way.