0

I have a map in which I want some values to be come on top.

HashMap<String,String> hm = new HashMap<String,String>(); 
hm.put("Phone Number","998291829");
hm.put("Address","12 A Street");
hm.put("Name","Robert Singh");
hm.put("Email","robert@gmail.com");
-
-

I want Name and email as first and second entry,How can I do this.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
vikas27
  • 563
  • 5
  • 14
  • 36
  • Do you want the items to be sorted in the way they are inserted? – Luiggi Mendoza Aug 29 '12 at 17:14
  • I understand he wants them in a specific order. – Dan D. Aug 29 '12 at 17:20
  • No Actually values and keys are coming from caouchdb, and i have to use this map to show on u.i. Where key is the label and value is the value next to label. – vikas27 Aug 29 '12 at 17:23
  • 1
    @vikasTheJavaDeveloper then you should follow the advice from Peter Lawrey: create a class that will have the attributes "name", "email", and others, then you fill it with the data and you can display it easily in your UI. – Luiggi Mendoza Aug 29 '12 at 17:29
  • @Dan something I've learned here on SO is that OP won't (always) write the real problem, instead the problem with the solution he/she tries to apply :). – Luiggi Mendoza Aug 29 '12 at 17:30
  • This is not 'sorting' but 'sequencing'. – user207421 Aug 30 '12 at 00:11

5 Answers5

2

HashMap is not a sorted or sortable collection.

What you can do is use TreeMap with your own Comparator.

However, I suspect you would be better off using a real class instead of an ad-hoc Map.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Instead of using HashMap, you should use a LinkedHashMap because it preserves the order you add the elements. You would have to add the elements in the order you need them.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

You can use a tree map. The constructor accepts a comparator.

You can pass your own implementation of the comparator where in you can implement the logic of ordering the data in your map.

Scorpion
  • 3,938
  • 24
  • 37
0

I would suggest that you create a bean e.g. Contact or Person and then write comparator for this.

So once you are done creating bean and comparator for it. Then

Populate the list/collection of bean

and the finally do

Collection.sort(yourlist,sortByNameComparator);

Collection.sort(yourlist,sortByemailComparator);

Where sortByNameComparator and sortByemailComparator are the comparators you created.

Take a look at this. it will definitely solve your problem.

Sorting an ArrayList of Contacts based on name?

The above has example of creating comparators.

Community
  • 1
  • 1
Nomad
  • 1,092
  • 11
  • 29
  • 42
-1

Depending on your usage, you might want to use a List<Pair>, where Pair is something like

public class Pair<J,K> {
    private J key;
    private K value;

    public Pair(J key, K value) {
        this.key = key;
        this.value = value;
    }

    public J getKey() {return key;}
    public K getValue() {return value;}
}
Stuart
  • 307
  • 4
  • 9
  • How's this supposed to help OP in the current problem? Using this list will become useless if someone needs to use a fast and good `get` method. – Luiggi Mendoza Aug 29 '12 at 17:20