Java Map Reverse reverse(Map source)

Here you can find the source of reverse(Map source)

Description

Returns a new map that contains the reverse of the given map.

License

Apache License

Parameter

Parameter Description
source the map that is to be reversed

Return

the reverse of the given map

Declaration

public static <T> Map<T, T> reverse(Map<T, T> source) 

Method Source Code

//package com.java2s;
/*/*from   w w  w  . j av  a  2s . c  o m*/
 * Copyright 2013 OmniFaces.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * Returns a new map that contains the reverse of the given map.
     * <p>
     * The reverse of a map means that every value X becomes a key X' with as corresponding
     * value Y' the key Y that was originally associated with the value X.
     *
     * @param source the map that is to be reversed
     * @return the reverse of the given map
     */
    public static <T> Map<T, T> reverse(Map<T, T> source) {
        Map<T, T> target = new HashMap<T, T>();
        for (Entry<T, T> entry : source.entrySet()) {
            target.put(entry.getValue(), entry.getKey());
        }

        return target;
    }
}

Related

  1. reverse(Map kvMap)
  2. reverse(Map map)
  3. reverse(Map map)
  4. reverse(Map source, Map target)
  5. reverse(Map source)
  6. reverse_map(Map map)
  7. reverse_map(Map map)
  8. reverseKeyAndValue(Map map)
  9. reverseLabelMap(Map labelMap)