user2257461
user2257461

Reputation: 59

How to reverse a HashMap?

I need to reverse the order of what the below method returns. For example if it returns:

1=ball, 2=save, 3=take 4=till

I want to reverse and return:

1=till, 2=take, 3=save, 4=ball 

My method is:

  public Map<Integer, String> returnMapOfValues(ArrayList<String> wordsList) {
    int getFrequencyValue = 0;
    Set<String> uniqueSetWords = new HashSet<String>(wordsList);
    for (String temp : uniqueSetWords) {
        getFrequencyValue = Collections.frequency(wordsList, temp);
        //prints the Collection of words and they frequency, For testing only
        //System.out.println(temp + ":" + Collections.frequency(wordsList,
        //temp));
        map.put(getFrequencyValue, temp);
        getFrequencyValue = 0;
    }
    return map;
}

Upvotes: 1

Views: 12922

Answers (5)

dnocode
dnocode

Reputation: 2088

//map to arrange inizialized 
LinkedHashMap<String,object> mta;


//transforms into arrayList  hashmap keys and values

ArrayList<Object> valuesTMP=new ArrayList<Object>(mta.values());

ArrayList<String> keysTMP; 

keysTMP=new ArrayList<String>(mta.keySet());

//reverse 
Collections.reverse(valuesTMP);
Collections.reverse(keysTMP);

LinkedHashMap <String,Object> mtarranged=new LinkedHashMap<String, Object>();

 int index=0;

  for( String key : keysTMP){

        mtarranged.put(key,valuesTMP.get(index));
        index++;
    }

Upvotes: 2

Azhaguvel A
Azhaguvel A

Reputation: 629

Please find the below code and Let me know Which is suitable for you ...

        int iterationValue = 0;
        List<String> stringList = new LinkedList<String>();
        stringList.add("ball");
        stringList.add("save");
        stringList.add("tick");

        Map<Integer, String> map = new HashMap<Integer, String>(); 
        for (String temp : stringList) {
            map.put(iterationValue++, temp);
        }
        // Input MAP
        System.out.println(map); // {0=ball, 1=save, 2=tick}

        Collections.reverse(stringList);
        iterationValue = 0;
        for (String temp : stringList) {
            map.put(iterationValue++, temp);
        }
        // Output MAP
        System.out.println(map); // {0=tick, 1=save, 2=ball}

Upvotes: 0

Mohamed Idris
Mohamed Idris

Reputation: 442

I've made some changes to the code. Please check if this is OK for you now.

public void reverseMap()
{
NavigableMap<Integer,String> map = new TreeMap<Integer,String>();
LinkedHashMap<Integer,String> reverseMap = new LinkedHashMap<Integer,String>();
map.put(1,"Apple");
map.put(2,"Ball");
map.put(3,"Cat");
NavigableSet<Integer> keySet = map.navigableKeySet();
Iterator<Integer> forwardIterator = keySet.iterator();
Iterator<Integer> reverseIterator = keySet.descendingIterator();
Integer i;
Integer j;
while(forwardIterator.hasNext())
{
    i = forwardIterator.next();
    j = reverseIterator.next();
    reverseMap.put(i,map.get(j));
}
System.out.println(reverseMap);
}

Upvotes: 0

Rick Hanlon II
Rick Hanlon II

Reputation: 21667

To answer this question, we need to know how you're iterating the Map since Maps don't have entry order. However, you may try using a TreeMap which has the method descendingMap:

public Map<Integer, String> returnMapOfValues(ArrayList<String> wordsList) {
    TreeMap<Integer, String> map = new TreeMap<Integer, String>();
    int getFrequencyValue = 0;

    Set<String> uniqueSetWords = new HashSet<String>(wordsList);
    for (String temp : uniqueSetWords) {
        getFrequencyValue = Collections.frequency(wordsList, temp);
        //prints the Collection of words and they frequency, For testing only
        //System.out.println(temp + ":" + Collections.frequency(wordsList,
        //temp));
        map.put(getFrequencyValue, temp);
        getFrequencyValue = 0;
    }

    return map.descendingMap();
}

Edit: From your comment, your intent is more clear. A TreeMap will help you with your goal because it is a Map ordered by the natural order of the keys.

Upvotes: 0

Mohamed Idris
Mohamed Idris

Reputation: 442

Please check if the below code I've written is useful for you as a reference:

public void reverseMap()
{
    NavigableMap<Integer,String> map = new TreeMap<Integer,String>();
    LinkedHashMap<Integer,String> reverseMap = new LinkedHashMap<Integer,String>();
map.put(1,"Apple");
    map.put(2,"Ball");
map.put(3,"Cat");
NavigableSet<Integer> keySet = map.navigableKeySet();
Iterator<Integer> iterator = keySet.descendingIterator();
Integer i;
while(iterator.hasNext())
{
    i = iterator.next();
    reverseMap.put(i,map.get(i));
}
System.out.println(reverseMap);
}

Upvotes: 2

Related Questions