集册 Java实例教程 反转映射(切换键和值类型)。

反转映射(切换键和值类型)。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

367
反转映射(切换键和值类型)。
/**
nowjava 提供 
**/


//package com.nowjava;


import java.util.HashMap;


import java.util.Map;

import java.util.Map.Entry;


public class Main {

    /**

     * Reverses a map (switches key and value types).

     *

     * @param <K> the key type

     * @param <V> the value type

     * @param map the map

     *

     * @return the reversed map

     */

    public static <K, V> Map<V, K> reverse(final Map<K, V> map) {

        final Map<V, K> reversed = new HashMap<V, K>(map.size());

        for (
展开阅读全文