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

/**
 * Inverts the given {@link Map}, assuming a one-to-one key-value
 * correspondence./*from  ww  w  .  j a  v a2  s  .  c  om*/
 * 
 * @param map
 *            the {@code Map} to invert
 * @return inverted {@code Map}
 */
public static <K, V> Map<V, K> invert(Map<K, V> map) {
    Map<V, K> inv = new HashMap<V, K>();
    for (Entry<K, V> entry : map.entrySet())
        inv.put(entry.getValue(), entry.getKey());
    return inv;
}

From source file:Main.java

/**
 * Remove um string da tabela de espalhamento caso seja igual ao parametro 'newString'
 * //from  www  .j  a v a2s .c om
 * @param vehiclesMap
 */
public static void removeOutStringsStrings(Map<String, String> map, String newString) {
    for (Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
        String key = entry.getKey();
        //         List<String> value = entry.getValue();

        if (key.equals(newString)) {
            iterator.remove();
        }
    }

    //      System.out.println("Lista: " + vehicles.size());
}

From source file:Main.java

public static String serializeNamespaces(Map<String, String> namespaces) {
    String serialization = "";
    for (Entry<String, String> entry : namespaces.entrySet()) {
        String localPart = entry.getKey();
        String namespace = localPart.isEmpty() ? "xmlns" : ("xmlns:" + localPart);
        serialization = concatKeyValue(serialization, namespace, entry.getValue());
    }/*ww w  . j a  v  a2s  .c  om*/
    return serialization;
}

From source file:Main.java

/**
 * Look up giving map for giving value collect pick all suitable keys
 * @param map//  w  w  w  .java 2  s  .c o m
 * @param value
 * @return ste of keys mapped to the giving value
 */
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<T>();
    for (Map.Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            keys.add(entry.getKey());
        }
    }
    return keys;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> checkMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) {
    if (DEBUG) {// ww w.ja v  a2 s . co m
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            keyType.cast(entry.getKey());
            valueType.cast(entry.getValue());
        }
    }
    return (Map<K, V>) map;
}

From source file:Main.java

public static <TKey extends TKeyT, TKeyT> Map<? super TKey, Integer> mergeMapWithAdd(Map<TKeyT, Integer> target,
        Map<? extends TKey, ? extends Integer> source) {
    for (Entry<? extends TKey, ? extends Integer> entry2 : source.entrySet()) {
        TKey key2 = entry2.getKey();/*from   w  ww .  j  av a2 s  .c o  m*/
        int val2 = entry2.getValue();
        if (target.containsKey(key2)) {
            int val1 = target.get(key2);
            target.put(key2, val1 + val2);
        } else {
            target.put(key2, val2);
        }
    }
    return target;
}

From source file:Main.java

/**
 * Sorts given map by given comparator/*from  w w w.j av  a  2s  .c om*/
 *
 * @param map        The map
 * @param comparator The comparator to sort the map
 * @param <K>        The key type
 * @param <V>        The value type
 * @return The sorted map
 */
public static <K, V> Map<K, V> sortMapByValue(Map<K, V> map, Comparator<Map.Entry<K, V>> comparator) {
    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
    list.sort(comparator);

    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> Map<K, List<?>> copyNullSafeMultiHashMap(Map<? extends K, List<?>> map) {
    if (map == null)
        throw new NullPointerException("map");

    Map<K, List<?>> result = copyNullSafeMutableHashMap(map);
    for (Map.Entry<K, List<?>> entry : result.entrySet()) {
        List<?> value = entry.getValue();
        entry.setValue(Collections.unmodifiableList(new ArrayList<Object>(value)));
    }// ww  w  . ja v  a 2 s . co m
    return result;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Map<Integer, String> swapStrKeysAndIntValues(Map<String, Integer> hm) {
    HashMap<Integer, String> nhm = new HashMap<Integer, String>();
    Set<?> s = hm.entrySet();
    Iterator<?> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry m = (Map.Entry) it.next();
        nhm.put((Integer) m.getValue(), (String) m.getKey());
    }//ww  w .  java2s  . c  o  m
    return nhm;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Map<String, Integer> swapIntKeysAndStrValues(Map<Integer, String> hm) {
    HashMap<String, Integer> nhm = new HashMap<String, Integer>();
    Set<?> s = hm.entrySet();
    Iterator<?> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry m = (Map.Entry) it.next();
        nhm.put((String) m.getValue(), (Integer) m.getKey());
    }/*from w ww.jav  a 2s .  c  o  m*/
    return nhm;
}