import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Sort {
public static void main(String[] args) {
Map<String, Double> map = new HashMap<String, Double>();
map.put("A", 1.1);
map.put("F", 2.2);
map.put("C", 4.4);
map.put("D", 6.6);
map.put("E", 3.3);
List<Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(
map.entrySet());
Collections.sort(list, new Comparator<Entry<String, Double>>() {
public int compare(Entry<String, Double> entry1,
Entry<String, Double> entry2) {
if (entry1.getValue() < entry2.getValue())
return 1;
else if (entry1.getValue() > entry2.getValue())
return -1;
return 0;
// return
// (entry1.getKey()).toString().compareTo(entry2.getKey());//按键排序
}
});
for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i).toString());//按键排序
System.out.println(list.get(i));
}
}
}
Hashmap按键或值排序
最新推荐文章于 2021-04-20 09:18:14 发布