【问题标题】:Sort a Map in Java is not working在 Java 中对地图进行排序不起作用
【发布时间】:2012-05-06 05:05:41
【问题描述】:

我正在尝试使用 Comparator 接口对 TreeMap(将 Double 作为值和 Integer 值作为键)进行排序,但它不起作用。

// Create a tree map
        TreeMap tm = new TreeMap();
        // Put elements to the map
        tm.put(1, new Double(3434.34));
        tm.put(0, new Double(123.22));
        tm.put(4, new Double(1378.00));
        tm.put(2, new Double(99.22));
        tm.put(3, new Double(-19.08));
        List<Map.Entry> valueList = new ArrayList(tm.entrySet());

        // Collections.sort(valueList, new Sort());

        Collections.sort(valueList, new Sort());

        HashMap sortedMap = new HashMap();

        // Get an iterator
        Iterator<Map.Entry> i = valueList.iterator();

        // Display elements
        while (i.hasNext()) {
            Map.Entry object = i.next();
            sortedMap.put(object.getKey(), object.getValue());
        }
        List sortedList = new ArrayList(sortedMap.entrySet());
        Iterator<Map.Entry> iterator = sortedList.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            System.out.println("Value " + entry.getValue() + "\n");
        }

以下是我的比较器类

public class Sort implements Comparator<Map.Entry> {

    @Override
    public int compare(Map.Entry o1, Map.Entry o2) {
        // TODO Auto-generated method stub
        double valueOne = (Double) o1.getValue();
        double valueTwo = (Double) o2.getValue();

        int returnValue =
            valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);

        return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
    }

}

但我得到以下输出

Value 123.22

Value 3434.34

Value 99.22

Value -19.08

Value 1378.0

Edited Part

public int compare(Map.Entry o1, Map.Entry o2) {
        // TODO Auto-generated method stub
        double valueOne = ((Double) o1.getValue()).doubleValue();
        double valueTwo = ((Double) o2.getValue()).doubleValue();

        int returnValue =
            valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);

        return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
    }

【问题讨论】:

  • 你不应该将Double值与==符号进行比较,而是尝试比较&gt;&lt;,如果没有人返回值,则返回0
  • 感谢您的快速回答。我使用 Double Class 中的 doubleValue() 方法将值更改为 double,但它仍然没有得到排序。请参阅我的编辑部分。
  • JDK 中没有Map 实现可以让您按值排序。

标签: java sorting map


【解决方案1】:

HashMap 本质上是无序的。

相反,您可以首先使用比较器创建TreeMap

【讨论】:

    【解决方案2】:

    当您将它们放入 HashMap 时,它们将不会保留它们的顺序,因为 HashMap 不是有序映射。

    【讨论】:

      【解决方案3】:

      使用 HashMap 进行排序是行不通的,因为它不是一个列表。您需要查看 ArrayList 和 sort 方法。

      ArrayList<double> arr = new ArrayList<double>();
      sort(arr) //sort ascending 
      

      【讨论】:

        【解决方案4】:

        添加到其他人已经建议的内容,试试这个:

        import java.util.ArrayList;
        import java.util.Collections;
        import java.util.Comparator;
        import java.util.Iterator;
        import java.util.List;
        import java.util.Map;
        import java.util.Map.Entry;
        import java.util.TreeMap;
        
        public class SortDemo
        {
          public class Sort implements Comparator<Map.Entry>
          {
            public int compare(Entry o1, Entry o2)
            {
              Double valueOne = (Double) o1.getValue();
              Double valueTwo = (Double) o2.getValue();
              return (int) Math.signum(valueOne.compareTo(valueTwo));
            }
          }
        
          public static void main(String[] args)
          {
            new SortDemo().foo();
          }
        
          void foo()
          {
            TreeMap tm = new TreeMap();
            tm.put(1, new Double(3434.34));
            tm.put(0, new Double(123.22));
            tm.put(4, new Double(1378.00));
            tm.put(2, new Double(99.22));
            tm.put(3, new Double(-19.08));
        
            List<Map.Entry> valueList = new ArrayList(tm.entrySet());
            Collections.sort(valueList, new Sort());
        
            Iterator<Map.Entry> iterator = valueList.iterator();
            while (iterator.hasNext())
            {
              Map.Entry entry = iterator.next();
              System.out.println("Value: " + entry.getValue());
            }
          }
        }
        

        【讨论】:

          【解决方案5】:

          Comparator 仅适用于您的密钥,而不适用于条目。您需要一个Comparator&lt;Integer&gt;,并将此比较器的实例传递给TreeMap 构造函数。

          TreeMap<Integer,Double> tm = new TreeMap<Integer,Double>(myIntegerComparator);
          

          在您的示例中,您看到的行为是由于TreeMap 使用Integer 的标准比较(这是因为IntegerComparable&lt;Integer&gt;)。

          (顺便说一句,您还应该阅读泛型并在您的集合类以及任何其他参数化类中使用它们。)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多