【问题标题】:Getting sorted keys and values in the Map在 Map 中获取排序的键和值
【发布时间】:2013-01-25 10:15:52
【问题描述】:

希望不是重复问题,

我做了两个Class,“Dog”和“Ser”,

“狗”类首先使用比较器对价格进行排序。 (正确完成)
“Ser”类应在地图中添加排序价格表(作为值)+(未重复的国家代码作为“键”。我对我来说是正确的。

根据我的信息,Map 应该明确地按升序对键进行排序,但事实并非如此,请参阅输出。

我现在的目标是在 Class Ser 中使用相应的价格值对国家代码进行排序

class Dog implements Comparator<Dog>, Comparable<Dog> {
    private int CountryCode;
    private double Price;
    private String Operator;

    Dog() {
    }

    Dog(int c, double p, String o) {
        CountryCode = c;
        Price = p;
        Operator = o;
    }

    public int getCountryCode() {
        return CountryCode;
    }

    public double getPrice() {
        return Price;
    }

    public String getOperator() {
        return Operator;
    }

    // Overriding the compareTo method
    public int compareTo(Dog d) {

        double de = Price - d.getPrice();

        if (de > 0.00001)
            return 1;
        if (de < 0.00001)
            return -1;
        return 0;

    }

    // Overriding the compare method to sort by price
    public int compare(Dog d, Dog d1) {

        double de = d.getPrice() - d1.getPrice();

        if (de > 0.00001)
            return 1;
        if (de < 0.00001)
            return -1;
        return 0;
    }

    @Override
    public String toString() {
        return "  " + Price + ":" + Operator + "";
    }

}

public class Ser {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // Takes a list o Dog objects

        ArrayList<Dog> list1 = new ArrayList<Dog>();
        Map<Integer, Dog> mp = new HashMap<Integer, Dog>();

        list1.add(new Dog(1, 0.9, "A"));
        list1.add(new Dog(268, 5.1, "A"));
        list1.add(new Dog(46, 0.17, "A"));
        list1.add(new Dog(4620, 0.0, "A"));
        list1.add(new Dog(468, 0.15, "A"));
        list1.add(new Dog(4631, 0.15, "A"));
        list1.add(new Dog(4673, 0.9, "A"));
        list1.add(new Dog(46732, 1.1, "A"));

        list1.add(new Dog(1, 0.92, "B"));
        list1.add(new Dog(44, 0.5, "B"));
        list1.add(new Dog(46, 0.02, "B"));
        list1.add(new Dog(467, 1.0, "B"));
        list1.add(new Dog(48, 1.2, "B"));

        list1.add(new Dog(1, 0.9, "c"));
        list1.add(new Dog(44, 0.4, "c"));
        list1.add(new Dog(46, 0.01, "c"));
        list1.add(new Dog(467, 0.2, "c"));

        // Sorts the array list using comparator

        Collections.sort(list1, new Dog());

        System.out.println("Sort the Dog class with respect to Price");

        for (Dog a : list1)
            // printing the sorted list of ages
            System.out.println(a.getCountryCode() + "  : " + a.getPrice()
                    + "  : " + a.getOperator());

        // Add only those keys from sorted Price but if the key is added, don't
        // repeat it
        for (int i = 0; i < list1.size(); i++) {
            if (!mp.containsKey(list1.get(i).getCountryCode()))
                mp.put(list1.get(i).getCountryCode(), list1.get(i));

        }

        System.out.println("");

        System.out.println(" Get Sorted List of Keys and values");

        for (Map.Entry<Integer, Dog> e : mp.entrySet()) {

            System.out.println(e.getKey() + "" + e.getValue());

        }

    }
}

输出:根据价格对 Dog 类进行排序

4620  : 0.0  : A,  46  : 0.01  : c,  46  : 0.02  : B,  4631  : 0.15  : A,  468  : 0.15  : A,  46  : 0.17  : A,  467  : 0.2  : c,  44  : 0.4  : c,  44  : 0.5  : B,  1  : 0.9  : c,  4673  : 0.9  : A,  1  : 0.9  : A,  1  : 0.92  : B,  467  : 1.0  : B, 46732  : 1.1  : A, 48  : 1.2  : B, 268  : 5.1  : A

获取键和对应值的排序列表

4673  0.9:A, 1  0.9:c, 46732  1.1:A, 48  1.2:B, 4631  0.15:A, 4620  0.0:A, 468  0.15:A, 46  0.01:c, 467  0.2:c, 268  5.1:A, 44  0.4:c

【问题讨论】:

  • 首先,如果你实现了Comparable,你就不需要Comparator。其次,将price 存储在double 中是犯罪行为。
  • 使用名称为 CountryCode 而不是 countryCode 的变量也是犯罪行为;)
  • 根据java约定这是正确的说法
  • @Rohit Jain,我可以使用什么替代方案,而不是双倍?这样我仍然可以对价格进行排序

标签: java maps


【解决方案1】:

问题来了:

Map <Integer, Dog> mp=  new HashMap  <Integer, Dog> ();

HashMap 不会将其元素排序为任何顺序。 改用 TreeMap 试试。 TreeMap 有一个带有 Comparator 的构造函数。

http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多