1

I have a HashMap and I need sort him by key. What must I do? If you will give me example than I will be glad. Thank you for, anyway.

2 Answers 2

5

Pick an implementation of SortedMap (for example TreeMap), and :

  • If your keys implement Comparable use use the copy-constructor (new TreeMap(existingMap)) to put your existing map elements to the sorted map.
  • If your keys are not comparable, create the TreeMap with a Comparator and then use treeMap.addAll(existingMap)
Sign up to request clarification or add additional context in comments.

Comments

1

Use java.lang.TreeMap instead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.