2012-04-25 13 views
2

コンパイラインターフェイスを使用してTreeMap(値としてDoubleを値と整数値として持つ)をソートしようとしていますが、動作しません。Javaでマップを並べ替える

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

あなたが比較してみてください代わりに、 '=='記号でダブル値を比較べきではありません'>'と '<'、誰もあなたに値を返さなければ、0を返す –

+0

あなたの素早い答えをありがとう。ダブルクラスからdoubleValue()メソッドを使用して値をdoubleに変更しましたが、まだソートされていません。私の編集部分をご覧ください。 – Dilip

+0

JDKに値でソートできる 'Map'実装はありません。 –

答えて

1

他の人がすでに提案しているものに追加し、これを試してみてください。

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()); 
    } 
    } 
} 
+0

実例をいただきありがとうございます。 – Dilip

5

HashMapが本質的に順不同で取得しています。

代わりに、コンパレータで最初にTreeMapを作成することができます。

1

HashMapに入力すると、HashMapは順序付けられたマップではないため、注文を保持しません。

0

ソートにHashMapを使用すると、リストではないため、機能しません。あなたはArrayListとsortメソッドを調べる必要があります。

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

Comparator作品だけあなたの鍵、ないエントリ。 Comparator<Integer>が必要で、このコンパレータのインスタンスをTreeMapコンストラクタに渡します。あなたの例では

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

は、あなたが見るの行動が原因Integerの標準comparsionを使用してTreeMapIntegerComparable<Integer>あるので、これは動作します)にあります。

(ところで、あなたもジェネリックをよく読んで、あなたのコレクションクラス、ならびに任意の他のパラメータ化クラスでそれらを使用する必要があります。)

関連する問題