根据值在地图上排序

时间:2015-06-29 06:50:38

标签: java hashmap

如何根据值对哈希映射进行排序,如果值相同则,则排序应该在键上。

我尝试使用比较器,但它没有给出预期的结果。

我希望结果像这样

{Bajaj=8.0, Tata=7.99, Maruthi=6.34, Kmart=5.99, Honda=5.78, 
Adidas=4.99, Ford=3.99, Nike=3.99, Sears=3.99, Suzuki=3.99, 
Apple=2.99, Puma=1.99}

以下是完整的源代码:

import java.util.*;

public class Test {
    public static void main(String[] args) {

        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("Adidas", 4.99);
        map.put("Nike", 3.99);
        map.put("Puma", 1.99);
        map.put("Ford", 3.99);
        map.put("Apple", 2.99);
        map.put("Sears", 3.99);
        map.put("Kmart", 5.99);
        map.put("Tata", 7.99);
        map.put("Maruthi", 6.34);
        map.put("Honda", 5.78);
        map.put("Bajaj", 8.0);
        map.put("Suzuki", 3.99);

        System.out.println("unsorted map: " + map);

        sorted_map.putAll(map);

        System.out.println("results: " + sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with
    // equals.
    @Override
    public int compare(String a, String b) {
        if (base.get(a) > base.get(b)) {
            return -1;
        } else if (base.get(a) == base.get(b)) {
            System.out.println();
            if (a.compareTo(b) == -1) {
                return -1;
            } else if (a.compareTo(b) == 1) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

3 个答案:

答案 0 :(得分:2)

您的compare实施中存在两个问题。首先,您将框内Double值与==进行比较:

else if(base.get(a) == base.get(b))

您应该用

替换它
else if(base.get(a).equals(base.get(b)))

其次,您检查a.compareTo(b)以查找-11等具体值,但它可能会返回任何正数/负数。只返回a.compareTo(b)的结果更好更简单。这是固定的compare方法:

public int compare(String a, String b) {
    if (base.get(a) > base.get(b)) {
        return -1;
    } else if (base.get(a).equals(base.get(b))) {
        return a.compareTo(b);
    } else {
        return 1;
    } // returning 0 would merge keys
}

如果要以不区分大小写的方式对具有相同值的键进行排序,只需使用compareToIgnoreCase

public int compare(String a, String b) {
    if (base.get(a) > base.get(b)) {
        return -1;
    } else if (base.get(a).equals(base.get(b))) {
        return a.compareToIgnoreCase(b);
    } else {
        return 1;
    } // returning 0 would merge keys
}

答案 1 :(得分:0)

由于排序顺序依赖于值和键,因此请使用Map.Entry<String, Double>条目:

List<Map.Entry<String, Double>> entries = new ArrayList<>(base.entrySet());
Collections.sort(entries, new Comparator<Map<String, Double>>() {
    ...
});

答案 2 :(得分:0)

稍微扭曲了ValueComparator类的比较方法。这将首先对价值观进行排序。如果值相同则按键排序。希望这可以帮助。 输出将如下所示:

未分类的地图:{Adidas = 4.99,Bajaj = 8.0,Apple = 2.99,Ford = 3.99,Puma = 1.99,Tata = 7.99,Nike = 3.99,铃木= 3.99,本田= 5.78,Kmart = 5.99,Maruthi = 6.34 ,西尔斯= 3.99}

结果:{Bajaj = 8.0,Tata = 7.99,Maruthi = 6.34,Kmart = 5.99,Honda = 5.78,Adidas = 4.99,Ford = 3.99,Nike = 3.99,Sears = 3.99,Suzuki = 3.99,Apple = 2.99,彪马= 1.99}

import java.util.*;

public class SortValueMap {

    public static void main(String[] args) {

        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("Adidas", 4.99);
        map.put("Nike", 3.99);
        map.put("Puma", 1.99);
        map.put("Ford", 3.99);
        map.put("Apple", 2.99);
        map.put("Sears", 3.99);
        map.put("Kmart", 5.99);
        map.put("Tata", 7.99);
        map.put("Maruthi", 6.34);
        map.put("Honda", 5.78);
        map.put("Bajaj", 8.0);
        map.put("Suzuki", 3.99);

        System.out.println("unsorted map: " + map);

        sorted_map.putAll(map);

        System.out.println("results: " + sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with
    // equals.
    @Override
    public int compare(String a, String b) {
        if(base.get(a).compareTo(base.get(b)) != 0) {
            if (base.get(a) > base.get(b)) {
                return -1;
            } else { 
                return 1;
            }
        }
        return a.compareTo(b);
    }
}