get Map Key by value - Java java.util

Java examples for java.util:Map Value

Description

get Map Key by value

Demo Code


//package com.java2s;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**//  ww  w . j  a  va2  s  .com
     * 
     * @param <K>
     *            Type of Keys
     * @param <V>
     *            Type of Values
     * @param map
     *            a collections map
     * @param value
     * @return The Corresponding key for value in map
     */
    public static <K, V> K getMapKey(Map<K, V> map, V value) {
        for (Entry<K, V> e : map.entrySet()) {
            if (value.equals(e.getValue()))
                return e.getKey();
        }
        throw new IllegalArgumentException();
    }
}

Related Tutorials