I need help. I creating mini game plugin with map voting and I don't know how to do that.
HashMap<World, Integer> votes = new HashMap<World, Integer>();
Let's say the votes are closed and the server is choosing map. Which one? What when 2 maps will have the same biggest num. of votes?
Thanks for help, eNcoo.
Tie-Breaker Vote
Have another vote, but restrict choices to top worlds with equal high score.
Random Tie-Breaker
Use a random number generator to break the tie, such as shown in the following:
Map<World, Integer> votes = new HashMap<>();
...
// Get list of entries and sort descending by value
List<Entry<World, Integer>> entries = new ArrayList<>( votes.entrySet() );
Collections.sort( entries, (e1,e2) -> -e1.getValue().compareTo( e2.getValue() ) );
// Collect top scoring worlds
List<World> topWorlds = new ArrayList<>();
int highScore = entries.get( 0 ).getValue();
for ( Entry<World,Integer> e : entries )
if ( e.getValue() == highScore )
topWorlds.add( e.getKey() );
else
break;
// Pick one at random
World pick = topWorlds.get( new Random().nextInt( topWorlds.size() ) );
Oldest or Newest Vote
Tracking the timestamp of the oldest or newest vote for each world could also be used to break ties. For example, tracking the oldest vote gives preference to the first world vote on (in the set of ties), while the newest vote gives preference to the last world voted on. I am not sure if this is practical and mention it only as an exercise.