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 <T, U> void mapMergeRemove(Map<T, List<U>> map, Map<T, List<U>> mapToRemove) {
    for (Map.Entry<T, List<U>> e : mapToRemove.entrySet()) {
        if (map.containsKey(e.getKey())) {
            map.get(e.getKey()).removeAll(e.getValue());
            if (map.get(e.getKey()).isEmpty()) {
                map.remove(e.getKey());/*w  ww  .ja  v a 2 s. com*/
            }
        }
    }
}

From source file:Main.java

public static final <K, V> void printM(Map<K, V> map, String sep) {
    Iterator<Entry<K, V>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Entry<K, V> e = it.next();
        System.out.append(e.getKey().toString()).append("::").append(e.getValue().toString());
        if (it.hasNext()) {
            System.out.append(sep);
        }//from   ww w .j av a2  s. c  om
    }

}

From source file:Main.java

public static <Key, Value> Collection<Value> filterMapValuesByKey(Map<Key, Value> map,
        Predicate<Key> keyPredicate) {
    return map.entrySet().parallelStream().filter(entry -> keyPredicate.test(entry.getKey()))
            .map(Map.Entry::getValue).collect(Collectors.toList());
}

From source file:Main.java

/**
 * Get all the entries in the Map object out and place into
 * the List passed in, just add at the bottom.
 *
 * @param map The Map to get objects out of.
 * @param list The List to add objects to the end of.  We add the
 *             Map.Entry.//from   www.  jav  a2  s  . c  o  m
 */
public static void addMapEntriesToList(Map map, List list) {

    Iterator iter = map.entrySet().iterator();

    while (iter.hasNext()) {

        list.add(((Map.Entry) iter.next()).getValue());

    }

}

From source file:Main.java

public static <T, E> T getKeyByValue(java.util.Map<T, E> map, E value) {
    for (java.util.Map.Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }//from  w ww  .  j a va2s.  c o  m
    }
    return null;
}

From source file:Main.java

public static <T, E> T getKeyByValue(final Map<T, E> map, final E value) {
    for (final Map.Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }/*from www.  ja  v a  2  s  .c o m*/
    }
    return null;
}

From source file:Main.java

public static int sumIntegerValueInMap(Map<String, Integer> pMap) {
    Iterator<Entry<String, Integer>> cIte = pMap.entrySet().iterator();
    int vTotalSum = 0;

    while (cIte.hasNext()) {
        vTotalSum += cIte.next().getValue();
    }//www .  j a va  2  s  .co m

    return vTotalSum;
}

From source file:Main.java

public static void writeToWriter(Map m, Writer out) throws IOException {
    writeToWriter(m.entrySet().iterator(), out);
}

From source file:Main.java

public static <K, V> void addMapSet(Map<K, Set<V>> map, Map<K, Set<V>> toAdd) {
    for (Map.Entry<K, Set<V>> entry : toAdd.entrySet()) {
        Set<V> values = map.get(entry.getKey());
        if (values == null) {
            values = Sets.newHashSet();/*from www.  ja v a2  s .  c om*/
            map.put(entry.getKey(), values);
        }
        for (V value : entry.getValue()) {
            values.add(value);
        }
    }
}

From source file:Main.java

public static String hashMapToString(Map<? extends Object, ? extends Object> map) {
    Set<?> entrySet = map.entrySet();
    Iterator<?> it = entrySet.iterator();
    Map.Entry<? extends Object, ? extends Object> entry = null;
    StringBuilder strbldr = new StringBuilder();
    strbldr.append("{ ");
    while (it.hasNext()) {
        entry = (Entry<? extends Object, ? extends Object>) it.next();
        strbldr.append(entry.getKey() + ":" + entry.getValue() + ", ");
    }/*w w  w  .j a va 2  s.  com*/
    return new StringBuilder(strbldr.substring(0, strbldr.length() - 2)).append(" }").toString();
}