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 / 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

More Core Java Interview Questions

Explain about global variables in Java?

3 Answers  

This is my code i have a doubt class ab implements a,b { public void add() { System.out.println("Hi") } } interface a { public void add(); } interface b { public void add(); } in this code i have two interface implemented in the class has same method.just i want to know which method of interface implemented in the class. interface a or interface b? confused me .

3 Answers  

There are 2 different ways to create an object. a)By using keyword "new" b)By using Class.forName ("className").newInstance(); What is the difference between these 2 methods.

3 Answers  

what is meant by serialization?

7 Answers  

What is the difference between the synchronized() & static synchronized()?

2 Answers   HP, SparkTG,

What is the purpose of having the concept of overloading?

7 Answers   Ness Technologies,

What is the purpose of a statement block?

1 Answers  

Why should we use java?

1 Answers   IBM,

y cant i declare method like public final static show()

3 Answers  

Write java code to print "Hello how are you" Thread1 should have "Hello" Thread2 should have "how are you" both the threads should start at the same time

4 Answers   Huawei,

What advantage does an entity bean have over a sessin bean?

3 Answers   Adobe,

what is the difference between object and class

10 Answers   IBM,