TreeMapソート
1429 ワード
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapSort {
public TreeMap sort(HashMap map){
ValueComparator bvc = new ValueComparator(map);
TreeMap sorted_map = new TreeMap(bvc);
sorted_map.putAll(map);
return sorted_map;
}
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("A",99.5);
map.put("B",67.4);
map.put("C",67.4);
map.put("D",67.3);
TreeMap sorted_map = new MapSort().sort(map);
System.out.println("unsorted map: "+map);
System.out.println("results: "+sorted_map);
}
}
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}