tags:

views:

486

answers:

7

Hi All !

I have hashmap and its keys are like "folder/1.txt,folder/2.txt,folder/3.txt" and value has these text files data.

Now i am stucked. I want to sort this list. But it does not let me do it :( Here is my hashmap data type:

HashMap<String, ArrayList<String>> 

following function work good but it is for arraylist not for hashmap.

Collections.sort(values, Collections.reverseOrder());

I also tried MapTree but it also didn't work, or may be I couldn't make it work. I used following steps to sort the code with maptree

HashMap testMap = new HashMap();
Map sortedMap = new TreeMap(testMap);

any other way to do it??

I have one doubt as my keys are (folder/1.txt, folder/2.txt ) may be that's why?

+2  A: 

Use a TreeMap, and implement the Comparator interface for the folder paths.

The comparator should compare two keys according to what ever rules you want, and pass that comparator to the constructor of the TreeMap. If sorting by pure alphabetical rules are ok, then you can skip this step. If you want to do something special with the paths, then you will need to define what that is in the comparator.

MeBigFatGuy
Tree map sorts the *keys* not the elements!
Adrian
why would you want to sort the values? That doesn't make any sense.
MeBigFatGuy
You wrote "elements" first. Now it sounds okay, but SO wont let me take away the downvote until you edit again!
Adrian
+3  A: 

Why not just do this?

Map<String, ValueObject> testMap = new TreeMap<String, ValueObject>();

where ValueObject is whatever class you're using for values.

Edit: this is based on some assumptions -- waiting to get more information to see what OP really needs.

Kaleb Brasee
I did read the post -- he doesn't explicitly say whether he wants to sort based on the keys or the values. Neither does he say if the keys are Strings or Files. So I assumed he wants to sort based on String keys, which would be automatic with a TreeMap, which he should just use instead of a HashMap.
Kaleb Brasee
A: 

My first guess was that use use File objects rather than String objects as keys. But then I noted that you say your key is "folder/1.txt,folder/2.txt,folder/3.txt" and your values are of type Collection<Strings>. If this is so, maybe your solutions should be

map.put("folder/1.txt", ...);
map.put("folder/2.txt", ...);
map.put("folder/3.txt", ...);

rather than

map.put("folder/1.txt,folder/2.txt,folder/3.txt", new ArrayList(...));

to get the single files sorted with a TreeMap as you tried.

Adrian
He says his keys are Strings...
Kaleb Brasee
But he seems to use comma separated filenames as keys, so I edited my post to have another guess. Please remove downvote.
Adrian
Sure thing, we'll see if he ever clarifies what exactly he's doing :)
Kaleb Brasee
A: 

I believe HashMaps do not guarantee any ordering when you iterate through the keys. The iteration order depends on the buckets and collisions.

You may want to put the values into some other collection and then sort them.

jkeesh
HashMaps do NOT guarantee ordering in the keys. According to http://java.sun.com/javase/6/docs/api/java/util/HashMap.html "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."
Chip Uni
true for HashMap itself but have a look at LinkedHashMap (which extends HashMap). It guarantees order of keys like they were inserted.
manuel aldana
+6  A: 

I guess that you want the list of keys sorted.

If your hashmap is called h, then try this:

SortedSet<String> sortedKeys = new TreeSet<String>(h.keySet());
Chip Uni
thanks, It worked !sorry for if my question made your guys guess!
(-1)You can't new SortedSet(), it's an interface. Should be something like sortedKeys = new TreeSet<String>(h.keySet());
RealHowTo
RealHowTo -- Thank you. You're right. I have fixed my answer.
Chip Uni
Of course you can "new" an interface - it just means that you have to provide the entire implementation.
duffymo
(+1) for the fix !
RealHowTo
A: 

Sometimes I use LinkedHashMap, when calling keySet() it gives you back items in a sorted way, as you added the to the map through put().

manuel aldana
A: 

Use SortedMap().

Artic