user725803
user725803

Reputation: 237

Sort for HashMap<String, String> in Android

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.

Upvotes: 1

Views: 780

Answers (2)

Bozho
Bozho

Reputation: 597076

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)

Upvotes: 5

Lukas Eder
Lukas Eder

Reputation: 220797

Use java.lang.TreeMap instead

Upvotes: 1

Related Questions