How to sort a HashMap into a leaderboard.

Status
This thread has been locked.
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

kampai

Feedback score
8
Posts
361
Reactions
187
Resources
0
Java 8 :D

Code:
    public List<PlayerFaction> getTopFactions(int i) {
        return playerFactions().stream().sorted(Comparator.comparingInt(PlayerFaction::getOnlineSize)).limit(i).collect(Collectors.toList());
    }
If you used parallel it'd be pretty nice
 

Bryce

Senior Java & Full-Stack Developer
Supreme
Feedback score
18
Posts
678
Reactions
934
Resources
19
I'd suggest using a TreeMap. A TreeMap implements a SortedMap and NavigatableMap. This means, when items are added to the TreeMap, they will hold their natural order. A HashMap however, you cannot guarantee the map will hold its order. Essentially, If you are sorting, it is highly suggested that you use a TreeMap as the order will be pure and entirely up to you on how its sorted.
 
Last edited:

Canada_

Banned
Feedback score
0
Posts
108
Reactions
48
Resources
0
I'd suggest using a TreeMap. A TreeMap implements a SortedMap and NavigatableMap. This means, when items are added to the TreeMap, they will hold their natural order. A HashMap however, you cannot guarantee the map will hold its order. Essentially, If you are sorting, it is highly suggested that you use a TreeMap as the order will be pure and entirely up to you on how its sorted.
Thank you <3
 
Banned forever. Reason: Creating Multiple Accounts (Roman Bruce)
Status
This thread has been locked.
Top