I have a sorting issue with a Hashmap. My constraint is that
I MUST use the Hashmap and work with existing code. I do a
database query and place the results in a Hashmap. When I
iterate thru the Hashmap, it loses the original alphabetical
sorting done by the database. So, my problem is that I must
sort the results coming out of the Hashmap which is then
placed into another class.

Answer Posted / murali.25

1) Use a tree map or construct a treemap from hashmap.
2) If you have used order by in the query to DB, use LinkedHashMap
3) Have a java class with attributes corresponding to fields from the database table. Implement comparator interface.

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

If a class is declared without any access modifiers, where can the class be accessed?

600


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

1908


Can a string be null?

544


what state does a thread enter when it terminates its processing? : Java thread

600


What is the declaration statement?

516






What is the difference between and ?

515


What are classloaders?

599


Convert a BST into a DLL and DLL to BST in place.

674


What is space character in java?

577


How to check if linked list contains loop in java?

459


Explain method overloading?

573


What is string pool in java?

567


What is the difference between length and length() method in java?

648


What are the three parts of a lambda expression? What is the type of lambda expression?

568


Is it possible to use string in the switch case?

555