cancel
Showing results for 
Search instead for 
Did you mean: 

Can the keys and values in Hash Map be put in one's desired Order?

Former Member
0 Kudos

Hi,

Can the keys and values in Hash Map or any other type of map be put in one's desired Order? I want to send the attributes of a node in a particular order to a Generic Print Component and want to get the attributes in same order and print them. When i am using Hash Map or Tree Map the order of key-value pairs is changed accoding to hash code or Alphabetical order of keys respectively.

Regards,

Smriti.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Smriti,

Have you tried java.util.LinkedHashMap? The keys are saved in the order they were introduced to the map. For the rest, it works as a HashMap.

I hope this can help you.

Eneko.

former_member186016
Active Contributor
0 Kudos

The order cann't be maintained.

HashMap and Hashtable both are not ordered.

Regards,

Ashwani Kr Sharma

Former Member
0 Kudos

Hi Ashwani,

Is there any way to solve the concerned issues?

Regards,

Smriti.

former_member186016
Active Contributor
0 Kudos

Well you can use arraylist,vector for passing the values.

Other way can be let the HashMap or HashTable come on the other side and then sort it and store it in some structure to print the values.

Regards,

Ashwani Kr Sharma

Former Member
0 Kudos

Hi Ahswani,

Can you please tell me with the help of an example how to send Attribute Information via arrayList or Vector. I won't be able to sort the Hash map in a particular order on the other side as its being passed to a generic component and order will be different for all the maps being passed.

Thanks and Regards,

Smriti

former_member186016
Active Contributor
0 Kudos

Hi,

Well i think you generic print component accepts HashMap or HashTable.

If you can control over this, change the implementation so that it accepts ArrayList as input.

Can create a generic class . lets say "KeyValuePair" with field like Object key and Object value.

Create instances of this KeyValuePair to store the key and values. Now put them in ArrayList.

Now in print component if you get this arraylist which is ordered.

Regards,

Ashwani Kr Sharma

former_member186016
Active Contributor
0 Kudos

For sorting of HashMap:

http://www.theserverside.com/discussions/thread.tss?thread_id=29569

Regards,

Ashwani Kr Sharma