Java tutorial
//package com.java2s; /* * Copyright (C) 2013 * * 52North 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; } }