get key from Map By Value - Java java.util

Java examples for java.util:Map Value

Description

get key from Map By Value

Demo Code

// This program is free software: you can redistribute it and/or modify
//package com.java2s;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Main {
    public static <K, V> K getByValue(Map<K, V> map, V value) {
        Set<Entry<K, V>> entrySet = map.entrySet();
        K el = null;/*  ww w  . j a va 2 s  .  co m*/
        Iterator<Entry<K, V>> it = entrySet.iterator();
        while (it.hasNext()) {
            Entry<K, V> cur = it.next();
            if (cur.getValue().equals(value)) {
                el = cur.getKey();
                break;
            }
        }
        return el;
    }
}

Related Tutorials