11

I have map like that:

Map<Integer, MyEntry> map = new HashMap<Integer, MyEntry>();

MyEntry is that:

public class MyEntry {
    private String title;
    private String value;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

after putting values in map , I want to sort it. First element to be smallest and the last element to be biggest.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
grep
  • 5,465
  • 12
  • 60
  • 112

3 Answers3

18

For sorting by key, you can use a SortedMap - one common implementation is a TreeMap. Since Integers have a natural sort order, you don't need to do anything special other than just put them into a TreeMap

If you wanted to sort by values, there are a couple of techniques described in this question Sort a Map<Key, Value> by values (Java)

Community
  • 1
  • 1
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
10

If you want to sort you can use 2 types LinkedHashMap or most used is TreeaMap .

Map<Integer, MyEntry> map = new LinkedHashMap<Integer, MyEntry>();

OR

Map<Integer, MyEntry> map = new TreeMap<Integer, MyEntry>();

And to add some small sample, you can use this code:

Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "one");
map.put(3, "three");
map.put(2, "two"); 
// prints one two three   
for(Integer key : map.keySet()) {
   System.out.println(map.get(key);
}

Some usefull:

Sort using TreeMap Example

Another usefull sort example

Cheers,

MSA
  • 2,502
  • 2
  • 22
  • 35
  • LinkedHashMap sorts by insertion order or access order (that's probably not a natural order). A useful implementation but it won't help here. – Andreas Dolk Aug 25 '13 at 17:23
  • It will help you if you want to add sorted elements, if you have A,B,C this will always be the same if you add them in HashMap but in LinkedHashMap they will maintain the order. I just wanted to show that it is a good way to add sorted elements in LinkedHashMap, something that you can't do in HashMap – MSA Aug 25 '13 at 17:26
3

A HashMap does not guarantee the order after you sorted the map. If you want sort the map by keys, use TreeMap.

This might helpful:Order HashMap<String,Integer> according Integer

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    Are you sure a `HashMap` cannot be sorted? – Josh M Aug 25 '13 at 17:19
  • From the Java API docs `This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.` – Jeff Storey Aug 25 '13 at 17:20
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ I can tell. I would disagree with your first statement because even though you can't dynamically sort a `HashMap`, surely you could still sort the `HashMap` after the `HashMap` is modified (such as adding and removing `Entry`s) – Josh M Aug 25 '13 at 17:27
  • @JoshM Yes,we can sort it.But does not guarantee the order :).Yes my statement misleading that we cannot do it.But actually my intention is that sorting not guaranteed. Thanks. Edited :) – Suresh Atta Aug 25 '13 at 17:33
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Fair enough, but that is under the assumption that the `HashMap` is being modified in one way or another :). If you are manually sorting the keys yourself (after each `add` and `remove` invocation) and no further operations are performed on the `HashMap` to jeopardize the order, how might the order change? – Josh M Aug 25 '13 at 17:40
  • Just a note, I'm not claiming that sorting a `HashMap` after each `add`/`remove` operation is the best solution to what is being asked, I'm just simply disagreeing with your initial first proposition, and suggesting that a `HashMap` can appear to be sorted. :) – Josh M Aug 25 '13 at 17:43
  • @JoshM yes yes ,I understood your intention :) – Suresh Atta Aug 25 '13 at 17:45