Re-arrange HashMap keys in custom pre-defined order

Viewed 1601

I have an HashMap<String, String> that I need sorted in a specific order. I understand HashMaps, because of how they are constructed, cannot be easily sorted. However, I am looking for the best strategy to re-arrange the keys in my HashMap - for example my current HashMap looks like this:

"folioProperties": {
    "strategy": "Strategy",
    "summary": "Summary",
    "initiative": "Initiative",
    "deliveryMilestone": "Delivery Milestone",
    "onTarget": "On Target",
    "size": "Size",
    "driver": "Driver",
    "issueName": "Issue Name",
    "releaseWindow": "Release Window",
    "type": "Type",
    "targetQuarter": "Target Quarter"
}

I have a table view that shows each of these properties as columns and I need to order the columns/hashmap by Strategy, Initiative, Issue Name, Summary, Delivery Milestone, On Target, Size, Target Quarter, Driver, Type, Release Window.

Any suggestions would be appreciated!

P.S. Before the HashMap is constructed, the keys are in a List<String>.

2 Answers

It seems that you do not understand the problem that you are attempting to fix. The hashmap is a tool for storing information. It seems likely that you never actually need to sort the hashmap. Instead, it seems likely that you need to display elements in a specific order.

Here are two ways to retrieve the elements from the hashmap in a specific order:

Retrieve the elements in the desired order

blam = map.get("strategy");
... do something with blam.

blam = map.get("Initiative");
... do something with blam.

... repeat for all elements.

Sort the keys then retrieve them in order

keyList.addAll(map.keySet());
Collections.sort(keyList, yourComparator);
for (final String current : keyList)
{
    blam = map.get(current);
    ... do something with blam.
}

Stop representing an object as a Map

Build a class, perhaps named FolioProperties, that represents the properties of the Folio.

public class FolioPropertiesFND
{
    private String initiative;
    private String strategy;
    ... one field per property.

    public String getInitiative()
    {
        return initiative;
    }

    public void setInitiative(final String newValue)
    {
        initiative = newValue;
    }
}

If your data is in an ordered list prior to the map construction, then what you need is a java.util.LinkedHashMap

from the javadoc:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable), without incurring the increased cost associated with TreeMap

If you use a LinkedHashMap, then all you have to do is to put the key/value pairs in the correct order and the map will ensure the same iteration order for you.

Related