You need the candidates sorted by vote count descending. What is a practical Java approach?
Practice this question from Quiz: Collection Comparisons, with options and an explanation.
Question
You need the candidates sorted by vote count descending. What is a practical Java approach?
Option A — Correct answer
Sort a list copied from map.entrySet() using comparingByValue().reversed()
Map entries can be copied to a list and sorted by value.
Option B
Call HashMap.sort() directly
Option C
Use Character.isDigit()
Option D
Use volatile on the map
Explanation
Copy entrySet into a list and sort entries with comparingByValue().reversed().