Map集合中,關於取值和遍歷的相關操作


這是自己的關於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     }

歡迎批評,交流和指正,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM