1

I want to be able to sort the below HashMap by key, I have tried code like the following:

SortedSet<String> keys = new TreeSet<String>(HashMap.keySet());
for (String key : keys) { 
  // String value = map.get(key);
   // do something
}

But this brings back the error:

Cannot make a static reference to the non-static method keySet() from the type HashMap

If someone could point me in the right direction, that would be great, thanks!

The HashMap and it's key's:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_TITLE = "title";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
static final String KEY_PUBDATE = "pubDate";
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Callum
  • 21
  • 4
  • Duplicate , can you check this post http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java – dracula Aug 28 '14 at 08:13
  • You don't provide enough information but it seems that the HashMap is declared as an instance variable while the for-loop that's failing is called from a static context. – Nir Alfasi Aug 28 '14 at 08:14
  • 1
    Yeah, the error as being caused by the fact I was calling HashMap, instead of map....thanks! – Callum Aug 28 '14 at 08:18
  • Can you not just use class implementing `SortedMap<>`? see http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html – jdphenix Aug 28 '14 at 08:20

3 Answers3

2

You are making a call to the "keySet()" method using "HashMap" as if this is a static method. You need to call "keySet()" method on the map object but not through the class.

Change this

SortedSet<String> keys = new TreeSet<String>(HashMap.keySet());

to this

SortedSet<String> keys = new TreeSet<String>(map.keySet());
Ripu Daman
  • 2,482
  • 2
  • 18
  • 22
  • Ah yes, this seems to have fixed the error, thanks! So how would I go about sorting it by the keys then....what is the syntax for this? – Callum Aug 28 '14 at 08:17
  • you can refer the link http://www.java2s.com/Code/Java/Collections-Data-Structure/SortanHashMapbasedonthekeys.htm for sorting. – Ripu Daman Aug 28 '14 at 08:20
0
SortedSet<String> keys = new TreeSet<String>(HashMap.keySet());

keySet() is a non static method so you have to call keySet() on instance of HashMap.

So try this :

HashMap<String, String> test = new HashMap<String,String>();

String KEY_ITEM = "item";
String KEY_ID = "id";
test.put(KEY_ITEM, "value");
test.put(KEY_ID, "value");

SortedSet<String> keys = new TreeSet<String>(test.keySet());
Maxouille
  • 2,729
  • 2
  • 19
  • 42
0

You have two way to achieve this:

a.Use HashMap but convert the keySet into TreeSet

SortedSet<String> keys = new TreeSet<String>(map.keySet());

b.Use TreeMap instead, whose key has been sorted when you put any entries:

public static void main(String[] args){
    Map<String,String>map =new TreeMap<>();
    map.put("F", "F");
    map.put("C", "C");
    map.put("A", "A");
    map.put("ABC", "ABC");
    map.put("AB", "AB");
    Set<String> set=map.keySet();//been sorted      
    for (String key : set) { 
            System.out.println(map.get(key));
    }
}

output:

A
AB
ABC
C
F
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149