invertMap

 1 /**
 2      * 转换Map表中的key和value
 3      * @param <V> key的类型
 4      * @param <K> value的类型
 5      * @param map 转换前的map
 6      * @return 转换后的map
 7      */
 8     public static <V, K> Map<V, K> invertMap(Map<K, V> map)
 9     {
10         Map<V, K> out = new HashMap<V, K>(map.size());
11         for (Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); it
12                 .hasNext();)
13         {
14             Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
15             out.put(entry.getValue(), entry.getKey());
16         }
17         return out;
18     }

 

posted @ 2014-02-26 11:59  许仙儿  阅读(308)  评论(0编辑  收藏  举报