您的位置:首页 > 产品设计 > UI/UE

Map中根据value排序---输出value最大时所对应的Key

2013-05-23 12:02 645 查看
一   相关知识

        Map內涵资料是Key -> Value的架构集合体,而Key是属于Set的架构集合体,也就是说Key的值是唯一的,而Value的值可以重复。

        一般常用的子类是HashMap或TreeMap,

                           如果考虑效能的话,建议使用HashMap,

                           如果希望Key值有顺序性,就使用TreeMap吧!

二  根据Value的大小进行排序。(EntrySet+Comparator)

Map<String , Integer> mapInteger = new HashMap<String , Integer>();

mapInteger.put("A", 98);

mapInteger.put("B", 50);

mapInteger.put("C", 76);

mapInteger.put("D", 23);

mapInteger.put("E", 85);

map中对整数value值排序操作如下:

public static void mapSortInteger(Map<String , Integer> map){

List<Map.Entry<String, Integer>> listData = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

System.out.println("排序前listData:");

System.out.println(listData);

//排序----// //当value值为整型时,
 compare()方法


Collections.sort(listData, new Comparator<Map.Entry<String, Integer>>(){

public int compare(Map.Entry<String,Integer > o1, Map.Entry<String, Integer> o2){

return (o2.getValue() - o1.getValue());      

}

}

);
// //当value值为双精度浮点类型时,
 compare()方法

Collections.sort(listData,
new Comparator<Map.Entry<String, Double>>(){
public int compare(Map.Entry<String,Double
> o1, Map.Entry<String, Double> o2){
return
(int)
((o2.getValue() - o1.getValue()) * 1000);     
}

System.out.println("排序后listData:");

System.out.println(listData);

}

输出结果为:

 ========整型数据操作========

排序前listData:

[D=23, E=85, A=98, B=50, C=76]

排序后listData:

[A=98, E=85, C=76, B=50, D=23]

三  取得Value最大时对应的Key值的俩种方法

       1 通过EntrySet<K,V>,将value放到一个List中,利用集合方法类Collections的sort方法,进行排序.(默认自然升序),然后找出等于最大value的所有key.

public static void main(String[] args){

  Map map=new HashMap();

  map.put("d", 761);

  map.put("g", 7);

  map.put("a", 761);

  map.put("c", 34);

  int value=0;

     String maxKey = null;

     List list=new ArrayList();

     

  Iterator ite=map.entrySet().iterator();

  while(ite.hasNext()){

// EntrySet 将同一个元素的key与value 关联在一起,当当前元素的value等于按从小到大排序的最后(最大)的元素的value值

//,输出当前/元素的键key与值value



           Map.Entry entry =(Map.Entry)ite.next();

            value = Integer.parseInt(entry.getValue().toString());

            list.add(entry.getValue());

            Collections.sort(list);

             if(value == Integer.parseInt(list.get(list.size()-1).toString())){

                maxKey = entry.getKey().toString();

                System.out.println(maxKey+"/"+value);

             }

     }

 }

}

//          答案:

//          d/761

//          a/761

       2 先取的value的最大值,然后找出等于最大value的所有key。

public static void main(String[] args) {

  Map map=new HashMap();

  map.put("d", 761);

  map.put("g", 7);

  map.put("a", 761);

  map.put("c", 34);

  Set set=map.keySet();

  Iterator it=set.iterator();

  int max=0;

  while(it.hasNext()){

   String key=(String)it.next();

   int init=(Integer)map.get(key);

   if(max<(Integer)map.get(key)){

    max=(Integer)map.get(key);

   }

  }

  List<String> list=test(map,max);

  for(String s:list){

   System.out.print(s+"/");

   System.out.println(max);

  }

 }

 private static List test(Map map, Object o) {

  List list=new ArrayList();

  Set set=map.keySet();

  Iterator it=set.iterator();

  while(it.hasNext()){

   String key=(String)it.next();

     if(o.equals(map.get(key))){

     list.add(key);

    }

   } 

  return list;

 }

}

//          答案:

//          d/761

//          a/761

      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: