Sort hashmap

Hi all,I need to order data in a hashmap by id which is neither its key nor value. How can it be done?
[123 byte] By [denicea] at [2007-10-3 6:28:45]
# 1
By using a TreeMap instead a HashMap, as the latter does not support keeping any order. In a TreeMap you can define the Comparator you need.
stefan.schulza at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 2
A TreeMap will only sort the keys, though. So although you don't specifically need to use the key itself in the comparison, the comparator you specify would need to be able to do its job *based* on the keys.
Herko_ter_Horsta at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 3
Hm, wonder about the alternatives? Of course, the comparator needs access to the ids. Hence, an id must be derivable eithr from the key directly or via the related value. At least, there must be a mapping from key/value to an id.Some rubbish removed by: stefan.schulz
stefan.schulza at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 4
Hi guys,Thanks a lot for the info. But Im not very clear on how to pass back the value into tree map after sorting using comparator? Is there any sample coding available? Thanks
denicea at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 5
You don't pass anything back into the Map :) http://java.sun.com/developer/onlineTraining/collections/Collection.html#ComparatorInterface
stefan.schulza at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 6

But Im not very clear on how to pass back the value into tree map after sorting using comparator?

That's not the idea.

Create a TreeMap and pass your own custom Comparator to it (to the constructor of TreeMap). The Comparator is a class that implements interface Comparator and it decides the order of elements in the map.

After creating the TreeMap, add the elemens to the map in any order you like. The TreeMap automatically sorts them while you add them.

jesperdja at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 7
Hi,How do I assign comparator to treemap?I link the comparator to TreeMap like below and I got error.Thanks.Map attMap = new TreeMap(new Comparator());for (AttBean att : info.getAttributes()) {attMap.put(att.getName(), att.getValue());}
denicea at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 8
You might want to read this section of sun's Java Tutorials: http://java.sun.com/docs/books/tutorial/collections/interfaces/order.htmlor: http://www.developer.com/java/article.php/858411
stefan.schulza at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 9

Hi,

Thanks alot for your help. Really appreciate it.

I have another problem here.

I need to sort TreeMap with comparator using variable which is neither

the key nor the value of treemap.

So I create a treemap and assign the comparator to it.

Coding as below:

Map attMap = new TreeMap(new Comp());

for (AttBean attribute : a.getAttributes()) {

attMap.put(attribute.getName(), attribute.getValue());

}

--Error when comes to comparator

public class Comp implements Comparator {

public int compare (Object z1, Object z2) {

//to get id based on the key passed to comparator

--error occur

AttBean ab1 = (AttBean)z1;

AttBean ab2 = (AttBean)z2;

String a = ab1.getID().toString();

// Condition for comparator

// return compare result

}

}

/********************************************************

error:

java.lang.ClassCastException: java.lang.String

com.wgc.bo.wip.Comp.compare(Comp.java:22)

java.util.TreeMap.compare(TreeMap.java:1093)

java.util.TreeMap.put(TreeMap.java:465)

com.wgc.web.action.wip.WIP.doLoadDefaultValues(WIP.java:255)

Is the AttBean I used not compatible when sorting using comparator?

Any suggestion for the problem above?

Thanks.

denicea at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...
# 10
The arguments in compare are the keys not the values of the map's entries.
stefan.schulza at 2007-7-15 1:15:28 > top of Java-index,Core,Core APIs...