如何反转HashMap?

时间:2014-01-11 17:36:21

标签: java hashmap

我需要反转以下方法返回的顺序。例如,如果它 返回:

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

我想反转并返回:

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

我的方法是:

  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;
}

5 个答案:

答案 0 :(得分:2)

请检查我编写的以下代码是否对您有用:

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);
}

答案 1 :(得分:0)

要回答这个问题,我们需要知道你是如何迭代地图的,因为地图没有输入顺序。不过,您可以尝试使用TreeMap方法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();
}

编辑:从您的评论中,您的意图更加明确。 TreeMap会帮助您实现目标,因为它是按照按键的自然顺序排列的地图。

答案 2 :(得分:0)

我对代码做了一些更改。请检查现在是否可以。

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);
}

答案 3 :(得分:0)

请找到以下代码并告诉我哪个适合您...

        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}

答案 4 :(得分:0)

//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++;
    }