How to get 5 highest values from a hashmap?

前端 未结 6 1106
慢半拍i
慢半拍i 2021-02-06 14:49

I have a Hashmap that links a zipcodes stored as keys and population stored as values in a hashmap.

The hashmap contains around 33k entries.

I\'m trying to get t

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 15:09

    Try this, using standard methods and assuming that the population count is stored as Integers in the HashMap:

    List list = new ArrayList(zipCodePop.values());
    Collections.sort(list, Collections.reverseOrder());
    List top5 = list.subList(0, 5);
    

提交回复
热议问题