Here you can find the source of reverse(final Map
Parameter | Description |
---|---|
K | the key type |
V | the value type |
map | the map |
public static <K, V> Map<V, K> reverse(final Map<K, V> map)
//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; } }