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

public static void unregisterNamespaceUpdates(Map<String, String> updates) {
    NAMESPACE_UPDATES.entrySet().removeAll(updates.entrySet());
}

From source file:Main.java

public static <Key, Value> boolean mapKeysContain(Map<Key, Value> map,
        Predicate<Map.Entry<Key, Value>> predicate) {
    return map.entrySet().parallelStream().anyMatch(predicate);
}

From source file:Main.java

public static <K, V> void invert(Map<K, V> in, Map<V, K> out) {
    for (Map.Entry<K, V> e : in.entrySet()) {
        out.put(e.getValue(), e.getKey());
    }//from  www. ja  va 2 s  .c om
}

From source file:Main.java

/**
 * Puts all the values in the given source {@code Map} into the
 * destination {@code Map}, merging any {@code Map} values.
 *//*from w  w w  . j a  v a2 s  . com*/
@SuppressWarnings("unchecked")
public static void putAllRecursively(Map<String, Object> destination, Map<String, Object> source) {
    for (Map.Entry<String, Object> e : source.entrySet()) {
        String key = e.getKey();
        Object srcValue = e.getValue();
        Object dstValue = destination.get(key);
        if (srcValue instanceof Map && dstValue instanceof Map) {
            putAllRecursively((Map<String, Object>) dstValue, (Map<String, Object>) srcValue);
        } else {
            destination.put(key, srcValue);
        }
    }
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }/*  w w  w .  ja va  2  s  .c o  m*/
    });

    Map<K, V> result = new LinkedHashMap<>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:Main.java

public static <K, V> Map<K, V> copyNullSafeMutableHashMap(Map<? extends K, ? extends V> map) {
    if (map == null)
        throw new NullPointerException("map");

    Map<K, V> result = new HashMap<K, V>(map);
    for (Map.Entry<K, V> entry : result.entrySet()) {
        if (entry.getKey() == null)
            throw new NullPointerException("entry.getKey()");
        if (entry.getValue() == null)
            throw new NullPointerException("entry.getValue()");
    }//from  w ww .  ja  va 2 s.  c  o  m
    return result;
}

From source file:Main.java

/**
 * sort Map by key desc//from  w  ww . j a  va  2s.  c  o  m
 * 
 * @param unsortMap
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map sortByComparator(Map unsortMap) {

    List list = new LinkedList(unsortMap.entrySet());

    // sort list based on comparator
    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey());
        }
    });

    // put sorted list into map again
    Map sortedMap = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

From source file:Main.java

public static <V, K> Map<V, K> invertMap(Map<K, V> map) {
    Map<V, K> inv = new HashMap<>();
    for (Map.Entry<K, V> entry : map.entrySet()) {
        inv.put(entry.getValue(), entry.getKey());
    }/*  w  ww . j a  v a2 s . co  m*/
    return inv;
}

From source file:Main.java

public static <T1, T2, T3> Map<T1, T3> merge(Map<T1, T2> map1, Map<T2, T3> map2, Map<T1, T3> map3) {
    for (Map.Entry<T1, T2> e1 : map1.entrySet()) {
        map3.put(e1.getKey(), map2.get(e1.getValue()));
    }//from  ww w .j  a  v  a2s .c  o m
    return map3;
}

From source file:Main.java

public static <K, V> K getKeyByValue(Map<K, V> map, V value) {
    K result = null;//from  w w w  . j a v  a 2 s  . co  m
    for (Map.Entry<K, V> entry : map.entrySet()) {
        if (Objects.equals(entry.getValue(), value)) {
            result = entry.getKey();
            break;
        }
    }

    return result;
}