Pergunta

Como eu posso ordenar uma treemap usando seus valores em vez da chave?

Foi útil?

Solução

Você pode não como comparador do TreeMap é executada contra as chaves só, por exemplo, ver este construtor .

De qualquer forma, você pode usar várias coleções, use o TreeMap (ou melhor HashMap) para procurar elementos de chaves, e ter um SortedSet para iterar sobre os valores.

Outras dicas

Aqui está uma solução:

public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator =  new Comparator<K>() {
        public int compare(K k1, K k2) {
            int compare = map.get(k2).compareTo(map.get(k1));
            if (compare == 0) return 1;
            else return compare;
        }
    };
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}

Note que o mapa é ordenada a partir do valor mais alto para o mais baixo.

O Google Guava proporciona uma TreeMultiMap .

Você também pode usar duas coleções. O que você está tentando realizar? Você pode explicar seus casos de uso?

Apache Commons Collections tem uma TreeBidiMap :

Esta classe garante que o mapa será tanto ordem da chave ascendente e ordem crescente de valor, classificado de acordo com a ordem natural para o de e classes do valor da chave.

Há uma porta java5-genéricos dele aqui .

Tente código abaixo funciona muito bem para mim. Você pode escolher tanto ascendente, bem como ordem decrescente para a classificação.

package com.rais;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class SortMapByValue
{
    public static boolean ASC = true;
    public static boolean DESC = false;

    public static void main(String[] args)
    {

        // Creating dummy unsorted map
        Map<String, Integer> unsortMap = new HashMap<String, Integer>();
        unsortMap.put("B", 55);
        unsortMap.put("A", 80);
        unsortMap.put("D", 20);
        unsortMap.put("C", 70);

        System.out.println("Before sorting......");
        printMap(unsortMap);

        System.out.println("After sorting ascending order......");
        Map<String, Integer> sortedMapAsc = sortByComparator(unsortMap, ASC);
        printMap(sortedMapAsc);


        System.out.println("After sorting descindeng order......");
        Map<String, Integer> sortedMapDesc = sortByComparator(unsortMap, DESC);
        printMap(sortedMapDesc);

    }

    private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order)
    {

        List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Entry<String, Integer>>()
        {
            public int compare(Entry<String, Integer> o1,
                    Entry<String, Integer> o2)
            {
                if (order)
                {
                    return o1.getValue().compareTo(o2.getValue());
                }
                else
                {
                    return o2.getValue().compareTo(o1.getValue());

                }
            }
        });

        // Maintaining insertion order with the help of LinkedList
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        for (Entry<String, Integer> entry : list)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }

    public static void printMap(Map<String, Integer> map)
    {
        for (Entry<String, Integer> entry : map.entrySet())
        {
            System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());
        }
    }
}

Você poderia tentar dar um comparador que compara valores em vez de chaves quando você cria o TreeMap.

    final TreeMap<Integer,String> tree = new TreeMap<Integer,String>();
    tree.put(1, "1");
    tree.put(2, "2");
    tree.put(3, "3");
    tree.put(4, "4");

    final TreeMap<Integer,String> treeSortedByValues = new TreeMap<Integer,String>(new Comparator<Integer>()
    {
        public int compare(Integer o1, Integer o2)
        {
            return tree.get(o1).compareTo(tree.get(o2));
        }
    });
    treeSortedByValues.putAll(tree);

    for ( Entry<Integer, String> e : treeSortedByValues.entrySet() )
    {
        System.out.println(e.getKey() + ": " + e.getValue());
    }

Os valores de swap e chaves.

Mais a sério, por favor fornecer algum contexto que você deseja alcançar. Talvez seja o suficiente para classificar depois de outro processamento estiver concluído.

Isso é eu ter feito isso ..

package Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

class MyComparator implements Comparator<Object> {

    public int compare(Object o1, Object o2) {
        return (((Integer) o2).compareTo((Integer) o1));
    }
}

class MyComparator1 implements Comparator<Object> {
    Map<Integer, String> map;

    public MyComparator1(Map<Integer, String> m) {
        this.map = m;
    }

    public int compare(Object o1, Object o2) {
        return (((String) map.get(o1)).compareTo((String) map.get(o2)));
    }
}

public class Map1 {
    public static void main(String[] args) {
        Map<Integer, String> hmap = new HashMap<Integer, String>();
        hmap.put(5, "Ashok");
        hmap.put(21, "Bhanu");
        hmap.put(7, "chaman");
        hmap.put(28, "dheeraj");
        hmap.put(761, "edison");
        hmap.put(1, "frank");
        hmap.put(-6, "gopal");
        hmap.put(78, "hari");
        System.out.println("Hash Map:" + hmap);
        Map<Integer, String> tmap = new TreeMap<>(hmap);
        System.out.println("Tree Map:" + tmap);
        MyComparator comp = new MyComparator();
        Map<Integer, String> itmap = new TreeMap<>(comp);
        itmap.putAll(hmap);
        System.out.println("Tree Map Inreverse order:" + itmap);
        Map<Integer, String> orderValuemap = new TreeMap<Integer, String>(new 
            MyComparator1(hmap));
            orderValuemap.putAll(hmap);
            orderValuemap.put(22,"hello");
        for(Entry<Integer, String> mp:orderValuemap.entrySet())
            System.out.println("Value : "+mp.getValue());
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top