Example usage for java.util Map entrySet

List of usage examples for java.util Map entrySet

Introduction

In this page you can find the example usage for java.util Map entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String getKeyByValue(Map<?, String> hm, String value) {
    Set<?> s = hm.entrySet();
    Iterator<?> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry m = (Map.Entry) it.next();
        if (m.getValue().equals(value))
            return m.getKey() + "";
    }/*  w w w.j a v a 2 s .  c  o m*/
    return null;
}

From source file:Main.java

public static <T, E> T getKeyByValue(Map<T, E> map, E value) {

    for (Entry<T, E> entry : map.entrySet()) {
        if (value != null && value.equals(entry.getValue())) {
            return entry.getKey();
        }//from   w  w  w.  j  av  a 2 s  .  co  m
    }
    return null;
}

From source file:Main.java

public static <K, V> Map<V, K> inverse(Map<K, V> m, Map<V, K> newMap) {
    for (Entry<K, V> entry : m.entrySet()) {
        newMap.put(entry.getValue(), entry.getKey());
    }/* ww  w.  jav a2  s . com*/
    return newMap;
}

From source file:Main.java

public static <T, U> void mapMergeAdd(Map<T, List<U>> map, Map<T, List<U>> mapToAdd) {
    for (Map.Entry<T, List<U>> e : mapToAdd.entrySet()) {
        if (!map.containsKey(e.getKey())) {
            map.put(e.getKey(), new ArrayList<U>());
        }/* w w  w  . j av a2  s.  c o  m*/
        map.get(e.getKey()).addAll(e.getValue());
    }
}

From source file:Main.java

public static Map filterNullValues(Map map) {
    Iterator<Map.Entry> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry next = iterator.next();
        if (next.getValue() == null) {
            iterator.remove();//from  w  w  w  .j a  v  a 2s .co m
        }
    }
    return map;
}

From source file:Main.java

/**
 * One to One way of finding Key from Value in Map
 * @param map/*  w w w  .  j  a va  2  s.c  o m*/
 * @param value
 * @returnkey
 */
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
    for (Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
    return map.entrySet().stream().sorted(comparing(Map.Entry::getValue)).collect(toLinkedHashMap());
}

From source file:Main.java

public static <K, V> K getKeyByValue(Map<K, V> map, V value) {
    Set<Map.Entry<K, V>> entrySet = map.entrySet();
    for (Map.Entry<K, V> entry : entrySet) {
        if ((entry.getValue() == value) || ((entry.getValue() != null) && (entry.getValue().equals(value)))) {
            return entry.getKey();
        }//from ww  w  .  j  a  v  a2s  . co m
    }
    return null;
}

From source file:Main.java

/**
 * Get a key from a map based on value/*from w ww .j  a  v  a 2s. co m*/
 * @param map
 * @param value
 * @param <T>
 * @param <E>
 * @return
 */
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
    for (Map.Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

From source file:Main.java

public static <T, G> T getByValue(final Map<T, G> map, final G value) {
    for (final Entry<T, G> entry : map.entrySet())
        if (entry.getValue().equals(value))
            return entry.getKey();
    return null;/*from ww  w .  j a  va  2  s.  co  m*/
}