Here you can find the source of reverse(Map
Parameter | Description |
---|---|
source | the map that is to be reversed |
public static <T> Map<T, T> reverse(Map<T, T> source)
//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; } }