Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. public List<LocationVote> getMaxTimestampAscending(List<LocationVote> locationVotesSince)
  2. {
  3. // map/reduce to max timestamp for a location
  4. Map<LocationKey, Optional<Long>> locationToTimestampDesc = locationVotesSince
  5. .stream()
  6. .collect(
  7. Collectors.groupingBy(
  8. LocationVote::getLocationKey,
  9. Collectors.mapping(LocationVote::getTimestampUtcEpochMilli, Collectors.maxBy(Comparator.reverseOrder())))
  10. )
  11. ;
  12.  
  13. // convert to list of locations that were mapped sorted by timestamp ascending
  14. return locationVotesSince.stream()
  15. .filter(vote -> {
  16. Optional<Long> time = locationToTimestampDesc.getOrDefault(vote.getLocationKey(), Optional.empty());
  17. return time.isPresent() && (time.get() == vote.getTimestampUtcEpochMilli());
  18. })
  19. .sorted(Comparator.comparingLong(LocationVote::getTimestampUtcEpochMilli))
  20. .collect(Collectors.toList());
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement