aspose file tools
The moose likes Java in General and the fly likes Sorting Keys(Value Objects) in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Sorting Keys(Value Objects) in HashMap " Watch "Sorting Keys(Value Objects) in HashMap " New topic
Author

Sorting Keys(Value Objects) in HashMap

Aman Mohd
Greenhorn

Joined: Feb 26, 2009
Posts: 13
Hi,

I have one HashMap with Student Objects. And i want to sort this map with student id, student name or any other field in student value object. How can I achieve this?

Student is a POJO Class with student id, student name, student age and some other fields with their respective setters and getters.

My requirement is such that I have to sort the hashmap based on any field of the Student class. This student class is the key in the hashmap

E.g.



I have made my Student class implement Comparable interface and overidden the compareTo method. In this i have just compared the Ids of the students.



I want to sort this map using the student name field or the student age field or any other field in the student object. How can i accomplish this? This HashMap will have hundreds of Student objects. All of them have to be sorted by any sort parameter which would be stored in the key part of the hashmap.


Thanks.

Amaan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Please UseCodeTags. You can use the edit button to add them.

A HashMap is always unsorted. If you need sorting check out TreeMap.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Aman Mohd
Greenhorn

Joined: Feb 26, 2009
Posts: 13
Well i know that... i guess i cudnt explain wat i needed... i need to sort the hashmap key attributes.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

Maybe you could research on TreeMap instead of HashMap..

Hope that helps.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

by the way there is a difference between sorting based on keys and sorting based on values.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

Aman Mohd wrote:Well i know that... i guess i cudnt explain wat i needed... i need to sort the hashmap key attributes.

You could do this: (1) put all the keys of the HashMap in a List, (2) sort the list however you like.

As Rob says, a HashMap is unordered - there is no way that you can put the keys into the HashMap itself in a particular order. Think of a HashMap as a bag with key-value pairs which are not in any particular order.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

Jesper Young wrote:
Aman Mohd wrote:Well i know that... i guess i cudnt explain wat i needed... i need to sort the hashmap key attributes.

You could do this: (1) put all the keys of the HashMap in a List, (2) sort the list however you like.

As Rob says, a HashMap is unordered - there is no way that you can put the keys into the HashMap itself in a particular order. Think of a HashMap as a bag with key-value pairs which are not in any particular order.


@Jesper,
how about the TreeMap solution ? (Besides the fact that the OP actually mentioned HashMap)
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

You could also do it with a TreeMap. A TreeMap orders its elements either by the "natural ordering" of the elements (in that case, the element class needs to implement the Comparable interface), or you can use the constructor of TreeMap that takes a Comparator object that determines the ordering.

But the original poster specifically asked about ordering a HashMap - that you can't do, because a HashMap isn't an ordered collection.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Sorting Keys(Value Objects) in HashMap
 
Similar Threads
Check one of the property of objects contains the given value
CODE: Dynamic Sorting on Multiple Indexes
Getting custom key from map based on comparator etc.
TreeSet - Comparable
Sorting arraylist on the basis of object