Java Map Reverse reverse(Map map)

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

Description

Reversed a map, making the key value and the value key.

License

Open Source License

Parameter

Parameter Description
K the type of key in the map to reverse. These will be the values in the returned map.
V the type of values in the map to revert. These will be the keys in the returned map.
map the Map to reverse.

Return

the reverse of map . A new will be returned where the keys from map will be the values and the values will be the keys.

Declaration

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

Method Source Code

//package com.java2s;
/**//w w  w .  j  av  a  2  s  . co m
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Reversed a map, making the key value and the value key.
     * @param <K> the type of key in the map to reverse. These will be the
     * values in the returned map.
     * @param <V> the type of values in the map to revert. These will be the
     * keys in the returned map.
     * @param map the {@link Map} to reverse.
     * @return the reverse of {@code map}. A new {@link Map} will be returned
     * where the keys from {@code map} will be the values and the values will
     * be the keys.
     */
    public static <K, V> Map<V, K> reverse(Map<K, V> map) {
        Map<V, K> reversedMap = new HashMap<V, K>();
        for (Map.Entry<K, V> entry : map.entrySet()) {
            reversedMap.put(entry.getValue(), entry.getKey());
        }
        return reversedMap;
    }
}

Related

  1. reverse(final Map map)
  2. reverse(Map source, Map target)
  3. reverse(Map source, Map target)
  4. reverse(Map in)
  5. reverse(Map kvMap)
  6. reverse(Map map)
  7. reverse(Map source, Map target)
  8. reverse(Map source)
  9. reverse(Map source)