import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Double> map = new HashMap<String, Double>();
map.put("1", 1.1);
map.put("2", 3.1);
map.put("3", 2.1);
map.put("4", 1.6);
map.put("5", 3.0);
System.out.println(map);
List<Map.Entry<String, Double>> wordMap = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
System.out.println(wordMap);
Collections.sort(wordMap, new Comparator<Map.Entry<String, Double>>() {// 根据value排序
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
double result = o2.getValue() - o1.getValue();
if (result > 0)
return 1;
else if (result == 0)
return 0;
else
return -1;
}
});
System.out.println(wordMap);
for(Map.Entry<String, Double> set:wordMap){
System.out.println(set.getKey() +" "+set.getValue());
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Double> map = new HashMap<String, Double>();
map.put("1", 1.1);
map.put("2", 3.1);
map.put("3", 2.1);
map.put("4", 1.6);
map.put("5", 3.0);
System.out.println(map);
List<Map.Entry<String, Double>> wordMap = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
System.out.println(wordMap);
Collections.sort(wordMap, new Comparator<Map.Entry<String, Double>>() {// 根据value排序
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
double result = o2.getValue() - o1.getValue();
if (result > 0)
return 1;
else if (result == 0)
return 0;
else
return -1;
}
});
System.out.println(wordMap);
for(Map.Entry<String, Double> set:wordMap){
System.out.println(set.getKey() +" "+set.getValue());
}
}
}
{1=1.1, 2=3.1, 3=2.1, 4=1.6, 5=3.0}
[1=1.1, 2=3.1, 3=2.1, 4=1.6, 5=3.0]
[2=3.1, 5=3.0, 3=2.1, 4=1.6, 1=1.1]
2 3.1
5 3.0
3 2.1
4 1.6
1 1.1