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

Why is a constant variable important?

701


How would you use Bubble Sort to sort the number of elements?

826


What is the use of isempty in java?

777


What is Classloader in Java?

842


How do you sort objects in java?

750


how to create daemon thread in java?

842


What is singleton class and how can we make a class singleton?

876


What is module with example?

697


What is default size of arraylist in java?

786


What are some characteristics of interference class?

771


what is the significance of listiterator in java?

829


What is the internal implementation of set in java?

708


Does java support Operator Overloading?

834


Why can't we override private static methods?

745


How do generics work in java?

710