TreeMap默认按照Key排序,我现在想按照Value排序,用Comparator实现,怎么写?求助大神 5
2个回答
展开全部
final Map<String,Integer> map = new HashMap<String,Integer>();
map.put("语文课本",11);
map.put("英语课本",9);
map.put("数学课本",20);
map.put("政治课本",12);
map.put("化学课本",12);
System.out.println(map.toString());
//结果为{政治课本=12,英语课本=9,数学课本=20,语文课本=11,化学课本=12}
Map<String,Integer> orderByValue=new TreeMap<String,Integer>(
new Comparator<String>(){
public int compare(String obj1,String obj2){
if(map.get(obj1)!=map.get(obj2)){
return map.get(obj2).compareTo(map.get(obj1));
}else{
return map.get(obj1);
}
}
}
);
orderByValue.putAll(map);
System.out.println(orderByValue.toString());
//结果为{数学课本=20,政治课本=12,化学课本=12,语文课本=11,英语课本=9}
展开全部
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
public class SortMapByValue {
public static void main(String[] args){
TreeMap<String,Integer> newMap=new TreeMap<String,Integer>();
newMap.put("dsaf1", 1);
newMap.put("dsaf9", 9);
newMap.put("dsaf4", 4);
newMap.put("dsaf7", 7);
newMap.put("dsaf6", 6);
newMap.put("dsaf3", 3);
newMap.put("dsaf10", 10);
ByValueComparator bvc =new ByValueComparator(newMap);
List<String> newList=new ArrayList<String>(newMap.keySet());
Collections.sort(newList, bvc);
for(String str:newList){
System.out.println(str+"================================="+newMap.get(str));
}
}
/**
* @description
* 对HashMap按值进行排序
*
* */
static class ByValueComparator implements Comparator<String> {
TreeMap<String, Integer> base_map;
public ByValueComparator(TreeMap<String, Integer> base_map) {
this.base_map = base_map;
}
public int compare(String arg0, String arg1) {
if (!base_map.containsKey(arg0) || !base_map.containsKey(arg1)) {
return 0;
}
if (base_map.get(arg0) < base_map.get(arg1)) {
return 1;
} else if (base_map.get(arg0) == base_map.get(arg1)) {
return 0;
} else {
return -1;
}
}
}
}
这样行吗 你看看
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
public class SortMapByValue {
public static void main(String[] args){
TreeMap<String,Integer> newMap=new TreeMap<String,Integer>();
newMap.put("dsaf1", 1);
newMap.put("dsaf9", 9);
newMap.put("dsaf4", 4);
newMap.put("dsaf7", 7);
newMap.put("dsaf6", 6);
newMap.put("dsaf3", 3);
newMap.put("dsaf10", 10);
ByValueComparator bvc =new ByValueComparator(newMap);
List<String> newList=new ArrayList<String>(newMap.keySet());
Collections.sort(newList, bvc);
for(String str:newList){
System.out.println(str+"================================="+newMap.get(str));
}
}
/**
* @description
* 对HashMap按值进行排序
*
* */
static class ByValueComparator implements Comparator<String> {
TreeMap<String, Integer> base_map;
public ByValueComparator(TreeMap<String, Integer> base_map) {
this.base_map = base_map;
}
public int compare(String arg0, String arg1) {
if (!base_map.containsKey(arg0) || !base_map.containsKey(arg1)) {
return 0;
}
if (base_map.get(arg0) < base_map.get(arg1)) {
return 1;
} else if (base_map.get(arg0) == base_map.get(arg1)) {
return 0;
} else {
return -1;
}
}
}
}
这样行吗 你看看
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询