Search code examples
comparatorcomparable

Sorting in java for Map?



As we use Collections.sort(list) for Collection but can we able to use that in the case of map like by implementing Comparable and Comparator? Or we can just do iterator over a Map in java?

========================================================================


Solution

  • An important feature of a list is that there is an order to the elements, whereas within a Map there is no such order. Therefore it doesn't make sense to sort a Map. If order is important to you, you really should be using a list.