How to sort a hashmap by the attributes of the objects contained in it

I am trying to figure out how to sort a hashmap by one of the attributes of the objects stored in it.

for example, the objects stored in the hashmap are of the class 'booking', and each one has a variable 'date' associated with it... this 'date' is a SimpleDateFormat("dd/MM/yy, hh:mm a").

when i print the contents of the hashmap, i want the values to be printed out in order of date.

thankyou in advance...

[440 byte] By [kabadishaa] at [2007-9-24]
# 1

Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

However, the TreeMap class guarantees that the map will be in ascending key order.

If you need to iteratet through values in a specific order, you could extract all values from the map into a SortedSet and using a suited Comparator, something like : SortedSet values = new TreeSet(myDateComparator);

values.addAll(myMap.values());

for(Iterator it = values.iterator(); it.hasNext(); ) {

...

}

TimTheEnchantora at 2007-7-13 > top of java,Core,Core APIs...
# 2

You could order the objects "booking" using the Comparable or Comparator interface: you have to implement the CompareTo (Object obj) or Compare(Object a, Object b) Method.

In this way you can define one "natural" sort method for a List of "booking" objects and, if necessary, you can write specific sortings.

Refer to Collections.sort(...)

giangioa at 2007-7-13 > top of java,Core,Core APIs...
# 3
Hi,A HashMap is not sorted, so you need to put the values in some other kind of structure before you print them.Kaj
kajbja at 2007-7-13 > top of java,Core,Core APIs...
# 4
Write an appropriate Comparator and then construct a newTreeMap given this Comparator. Next putAll() your HashMapto it and you'll end up with a SortedMap that reflects the orderdefined by your Comparator.kind regards,Jos
JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 5

> Write an appropriate Comparator and then

> construct a new

> TreeMap given this Comparator. Next

> putAll() your HashMap

> to it and you'll end up with a SortedMap that

> reflects the order

> defined by your Comparator.

>

> kind regards,

>

> Jos

Won't that sort on the keys, and not on the values? I think he should use a TreeSet just before printing.

Kaj

kajbja at 2007-7-13 > top of java,Core,Core APIs...
# 6

> Won't that sort on the keys, and not on the values? I

> think he should use a TreeSet just before printing.

Yes it would, but isn't that exactly what the OP wanted then? I didn't

read anything about sorting the stuff according to the value objects;

even more, some values could 'disappear' in the inverted map, e.g.

(A,1) (B,1) (C,2) would come out as (1,A) (2,C) ...

kind regards,

Jos

JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 7

> Won't that sort on the keys, and not on the values? I

> think he should use a TreeSet just before printing.

Yes it would, but isn't that exactly what the OP wanted then? I didn't

read anything about sorting the stuff according to the value objects;

even more, some values could 'disappear' in the inverted map, e.g.

(A,1) (B,1) (C,2) would come out as (1,A) (2,C) ...

kind regards,

Jos

JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 8
I apologize four the double reply (shaky WiFi connection sometimes)kind regards,Jos
JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 9

> Yes it would, but isn't that exactly what the OP

> wanted then? I didn't

> read anything about sorting the stuff according to

> the value objects;

Is it?

>I am trying to figure out how to sort a hashmap by one of the attributes

>of the objects stored in it.

I read that as the values.

>for example, the objects stored in the hashmap are of the class 'booking',

Sounds strange to have booking as key, but I might be wrong.

>when i print the contents of the hashmap, i want the

>values to be printed out in order of date.

Date is an attribute of the booking (the value in the map)

Kaj

kajbja at 2007-7-13 > top of java,Core,Core APIs...
# 10
> Date is an attribute of the booking (the value in the map)Could be; I can't prove my wrong nor your right nor vice versa ;-)Let's wait 'till the OP clarifies matters.kind regards,Jos
JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 11
Hi guys, cheers for the help, just to clarify, the booking objects are the 'values' of the hashmap, not the keys.sorry for any confusion
kabadishaa at 2007-7-13 > top of java,Core,Core APIs...
# 12
No problem, I think the best solution remains to implement "booking" as Comparable, extract the values as a List and then sort them using Collections.Sort (in this way you could also order the list using more then one field, if needed).
giangioa at 2007-7-13 > top of java,Core,Core APIs...
# 13
ok, cool... sounds like a plan.big thanks to everyone who posted!i'll give it a try when i get a chance and post back success/faliure story :-)thanks again!
kabadishaa at 2007-7-13 > top of java,Core,Core APIs...
# 14

> Hi guys, cheers for the help, just to clarify, the booking objects are the

> 'values' of the hashmap, not the keys. sorry for any confusion

Ah, ok, so Kaj was right. Anyways most of what has been replied here

is still valid: create a TreeMap with an appropriate Comparator. You

do have to traverse your Hashmap manually though in order to put

your key/value pairs to the SortedMap as value/key pairs.

Realize though that some tuples could 'disappear', e.g. (A,1) (B,1) (C,2)

will come out as (1,A) (2,C) in the SortedMap.

kind regards,

Jos

JosAHa at 2007-7-13 > top of java,Core,Core APIs...
# 15
@OP. Do you want to print the keys and values, or only the values?Kaj
kajbja at 2007-7-21 > top of java,Core,Core APIs...
# 16
> @OP. Do you want to print the keys and values, or> only the values?Couldn't the SortedSet contain Map.Entry objects instead of Booking ones in this case?(Only the ad-hoc comparator would be slightly more complex.)
TimTheEnchantora at 2007-7-21 > top of java,Core,Core APIs...
# 17

i only need the values printed.

the objects have 5 variables each:

public String codeRef

private Date date/time//(dd/MM/yy, hh:mm a)

public String date/timeString

public String name

public String destination

the variable date/time is parsed into the string 'date/timeString'.

the objects have a Print method as follows:

public void Print()

{

System.out.println(

"Ref code: " + codeRef +

"\nDate/Time of Booking: " + date/timeString +

"\nName: " + name +

"\nDestination: " + destination);

}

i want to call the Print methods of each of the objects in the hashmap in such a way that they appear in order of date/time.

let me know if i havent explained very well..

cheers

kabadishaa at 2007-7-21 > top of java,Core,Core APIs...