Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<LocationVote> getMaxTimestampAscending(List<LocationVote> locationVotesSince)
- {
- // map/reduce to max timestamp for a location
- Map<LocationKey, Optional<Long>> locationToTimestampDesc = locationVotesSince
- .stream()
- .collect(
- Collectors.groupingBy(
- LocationVote::getLocationKey,
- Collectors.mapping(LocationVote::getTimestampUtcEpochMilli, Collectors.maxBy(Comparator.reverseOrder())))
- )
- ;
- // convert to list of locations that were mapped sorted by timestamp ascending
- return locationVotesSince.stream()
- .filter(vote -> {
- Optional<Long> time = locationToTimestampDesc.getOrDefault(vote.getLocationKey(), Optional.empty());
- return time.isPresent() && (time.get() == vote.getTimestampUtcEpochMilli());
- })
- .sorted(Comparator.comparingLong(LocationVote::getTimestampUtcEpochMilli))
- .collect(Collectors.toList());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement