国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

App下載
首頁javasortJava Data Structure - 如何排序地圖

Java Data Structure - 如何排序地圖

我們想知道如何排序地圖。
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;

class MyComparator 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 class Main {
  public static void main(String[] args) {
    TreeMap tm = new TreeMap();
    tm.put(1, new Double(344.34));
    tm.put(0, new Double(123.22));
    tm.put(4, new Double(138.00));
    tm.put(2, new Double(919.22));
    tm.put(3, new Double(-119.08));

    List<Map.Entry> valueList = new ArrayList(tm.entrySet());
    Collections.sort(valueList, new MyComparator());

    Iterator<Map.Entry> iterator = valueList.iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = iterator.next();
      System.out.println("Value: " + entry.getValue());
    }
  }
}