码迷,mamicode.com
首页 > 其他好文 > 详细

Map集合中,关于取值和遍历的相关操作

时间:2018-05-09 23:19:41      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:cti   return   测试   ati   获得   class   values   指正   param   

这是自己的关于map集合的相关操作的小研究,分享给大家。

主要代码内容包含以下:

1,map集合的遍历

2,根据key值获取value值

3,根据value值获取key值

4,返回最大value值对应的key值

5,获取最大key值,最小key值,最大value值,最小value值

上代码:

  1   @Test
  2     public void bb1(){//测试代码
  3         Integer value=0;
  4         Map<Integer,Integer> map=new HashMap<Integer,Integer>();
  5         map.put(1, 12);
  6         map.put(2, 13);
  7         map.put(4, 11);
  8         map.put(7, 22);
  9         map.put(3, 55);
 10         
 11         //map集合的遍历
 12         Iterator<Integer> i = map.keySet().iterator();
 13         
 14         while(i.hasNext()){
 15             Integer next = i.next();
 16             System.out.println("key2值:"+next+"----"+"value值:"+map.get(next));//key值是自然排序的
 17         }
 18         System.out.println("最大值Key值"+getMaxKey(map));
 19         System.out.println("最小值Key值"+getMinKey(map));
 20         System.out.println("最大值val值"+getMaxVal(map));
 21         System.out.println("根据value值获取key值:"+getKeyByVal(map,55));
 22         System.out.println("返回最大值value对应的key值:"+getKeyByMaxValue(map,value));
 23         System.out.println("根据key值获取对应的value值:"+getValByKey(map,1));
 24         
 25     }
 26     
 27     /**
 28      * 根据key值获取value值
 29      * @param map
 30      * @param key
 31      * @return
 32      */
 33     public static Object getValByKey(Map<Integer,Integer> map,Integer key){
 34         Integer value=0;
 35         for(Integer getVal:map.values()){
 36             if(getVal==map.get(key)){
 37                 value=getVal;
 38             }
 39         }
 40         return value;
 41     }
 42     /**
 43      * 求最大key值
 44      * @param map
 45      * @return
 46      */
 47     public static Object getMaxKey(Map<Integer,Integer> map){
 48         if(map==null){
 49             return null;
 50         }
 51             Set<Integer> set=map.keySet();
 52             Object[] obj = set.toArray();
 53             Arrays.sort(obj);//sort升序排序
 54             return map.get(obj[obj.length-1]);//获得最大key值对应的value值
 55 //        return obj[obj.length-1];
 56     }
 57     /**
 58      * 获取最大value值
 59      * @return
 60      */
 61     public static Object getMaxVal(Map<Integer,Integer> map){
 62         if(map==null){
 63             return null;
 64         }
 65         Collection<Integer> val = map.values();
 66         Object[] obj = val.toArray();
 67         Arrays.sort(obj);
 68 //        return obj[0];//获取最小value
 69         return obj[obj.length-1];//获取最大value
 70     }
 71     /**
 72      * 返回最小的key值
 73      * @param map
 74      * @return
 75      */
 76     public static Object getMinKey(Map<Integer,Integer> map){
 77         if(map==null){
 78             return null;
 79         }
 80         Set<Integer> set = map.keySet();
 81         Object[] obj = set.toArray();
 82         Arrays.sort(obj);
 83         return obj[0];
 84         
 85     }
 86      /**
 87       * 根据value值获取对应的key值
 88       * @return
 89       */
 90     public static String getKeyByVal(Map<Integer,Integer> map,Integer value){
 91         Integer key=0;
 92         for(Integer getKey:map.keySet()){
 93                 if(map.get(getKey)==value){
 94                     key=getKey;//这个key值最后一个满足条件的key值
 95                 }
 96             
 97         }
 98         return "value值为:"+value+"对应的key值为:"+key;
 99     }
100     /**
101      * 返回最大值对应的key值
102      * @param map
103      * @param value
104      * @return
105      */
106     public static Integer getKeyByMaxValue(Map<Integer,Integer> map,Integer value){
107         Integer maxVal =(Integer) getMaxVal(map);
108         value=maxVal;
109         Integer key=0;
110         for(Integer getKey:map.keySet()){
111             if(map.get(getKey)==value){
112                 key=getKey;
113             }
114         }
115         return key;
116     }

欢迎批评,交流和指正,谢谢!

Map集合中,关于取值和遍历的相关操作

标签:cti   return   测试   ati   获得   class   values   指正   param   

原文地址:https://www.cnblogs.com/jincheng81/p/9017190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!