Untitled
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
Map<Integer, Integer> findTopToKeepDatasorted(List<Entry> list) { HashMap<Integer, PlaceCount> ref = new HashMap(); // HashMap<Integer, TreeSet<PlaceCount>> inMemoryStruct = new HashMap(); for(Entry entry: list){ int postalCode = entry.postalCode; int placeId = entry.placeId; inMemoryStruct.putIfAbsent(postalCode, new TreeSet<>()); TreeSet<PlaceCount> placeCounts = inMemoryStruct.get(postalCode); ref.putIfAbsent(placeId, new PlaceCount(placeId, 0)); PlaceCount placeCount1 = ref.get(placeId); placeCounts.remove(placeCount1); placeCount1.count++; placeCounts.add(placeCount1); inMemoryStruct.put(postalCode, placeCounts); } Set<Map.Entry<Integer, TreeSet<PlaceCount>>> entries = inMemoryStruct.entrySet(); Map<Integer, Integer> ans = new HashMap<>(); for(Map.Entry<Integer, TreeSet<PlaceCount>> e:inMemoryStruct.entrySet()){ ans.put(e.getKey(), e.getValue().pollLast().placeId); } return ans; }
Editor is loading...