Java Map Reverse reverse(final Map map)

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

Description

Reverses a map (switches key and value types).

License

Open Source License

Parameter

Parameter Description
K the key type
V the value type
map the map

Return

the reversed map

Declaration

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

Method Source Code

//package com.java2s;
/*//  w  ww . ja  v a  2  s  .  co  m
 * Copyright (C) 2013
 *
 * 52?North Initiative for Geospatial Open Source Software GmbH
 * Contact: Andreas Wytzisk
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

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 (final Entry<K, V> e : map.entrySet()) {
            reversed.put(e.getValue(), e.getKey());
        }
        return reversed;
    }
}

Related

  1. reverse( Map map)
  2. reverse(Map source, Map target)
  3. reverse(Map source, Map target)
  4. reverse(Map in)
  5. reverse(Map kvMap)