• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Keys(Value Objects) in HashMap

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. You can use the edit button to add them.

A HashMap is always unsorted. If you need sorting check out TreeMap.
 
Aman Mohd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i know that... i guess i cudnt explain wat i needed... i need to sort the hashmap key attributes.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you could research on TreeMap instead of HashMap..

Hope that helps.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way there is a difference between sorting based on keys and sorting based on values.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic