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 <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(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 (o2.getValue()).compareTo(o1.getValue());
        }/*  w  w w .ja  v  a 2  s  .  co m*/
    });
    Map<K, V> result = new LinkedHashMap<K, V>();
    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> copyNullSafeMutableHashMapWithNullValues(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()");
    }/* w  w w  .  ja v a  2s .  co m*/
    return result;
}

From source file:Main.java

public static void printMap(Map<?, ?> m) {
    StringBuilder sb = new StringBuilder();
    for (Entry<?, ?> entry : m.entrySet()) {
        sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\r\n");
    }//from  w w w.  j a  va  2 s . c o  m
    Log.d("print", sb.toString());
}

From source file:Main.java

public static <Key, Value> Collection<Value> filterMapValues(Map<Key, Value> map,
        Predicate<Map.Entry<Key, Value>> entryPredicate) {
    return map.entrySet().parallelStream().filter(entryPredicate).map(Map.Entry::getValue)
            .collect(Collectors.toList());
}

From source file:Main.java

public static Map getMap(Map mapOld, Map newMap) {
    Iterator iter = mapOld.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        Object key = entry.getKey();
        Object obj = entry.getValue();
        if (obj instanceof Map) {
            getMap((Map) obj, newMap);
        } else {//w ww.  j  av a 2 s  .  c o m
            String key1 = (String) key;
            String value1 = (String) obj;
            if (!newMap.isEmpty()) {
                int nn = 0;
                Iterator iter1 = newMap.entrySet().iterator();
                while (iter1.hasNext()) {
                    Map.Entry entry1 = (Map.Entry) iter1.next();
                    String key2 = (String) entry1.getKey();
                    String[] arr1 = key2.split("\\|");
                    String e1 = arr1[0];
                    int n1 = Integer.parseInt(arr1[1]);
                    if (key1.split("\\|")[0].equals(e1) && n1 >= nn) {
                        nn = n1 + 1;
                    }
                }
                newMap.put(key1.split("\\|")[0] + "|" + nn, value1);
            } else {
                newMap.put(key1, value1);
            }
        }
    }
    return newMap;
}

From source file:Main.java

static void jsonObjectPutAll(JSONObject jsonObject, Map<String, Object> map) {
    Set<Map.Entry<String, Object>> entrySet = map.entrySet();
    for (Map.Entry<String, Object> entry : entrySet) {
        try {/* w w w  .jav  a2  s .c om*/
            jsonObject.putOpt(entry.getKey(), entry.getValue());
        } catch (JSONException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:Main.java

/**
 * Processes any attributes and add them into the element.
 *
 * @param element       the element where to add the attributes.
 * @param anyAttributes the any attributes to process.
 *//*www  .  j a  va 2 s .  c o m*/
public static void processAnyAttributes(Element element, Map<QName, String> anyAttributes) {
    for (Map.Entry<QName, String> attribute : anyAttributes.entrySet()) {
        String localName = attribute.getKey().getLocalPart();
        String prefix = attribute.getKey().getPrefix();
        String namespace = attribute.getKey().getNamespaceURI();
        element.setAttributeNS(namespace, prefix + ":" + localName, attribute.getValue());
    }
}

From source file:com.mirth.connect.util.BeanUtil.java

public static void setPropertiesQuietly(Object bean, Map<String, String> properties) {
    for (Entry<String, String> entry : properties.entrySet()) {
        try {/* ww  w  .  j av a2  s  . c  o  m*/
            BeanUtils.setProperty(bean, entry.getKey(), entry.getValue());
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

/**
 * This method returns the highest value of a map.
 * This value class of the map has to implement comparable
 *
 * @param map Map/*from w  w  w  . ja va2  s.  c o m*/
 * @param <K> Key class
 * @param <V> Value class implementing Comparable
 * @return Highest values in the map
 */
public static <K, V extends Comparable> V getHighestValue(Map<K, V> map) {
    V highestValue = null;

    for (Map.Entry<K, V> entry : map.entrySet()) {
        if (highestValue == null || entry.getValue().compareTo(highestValue) > 0) {
            highestValue = entry.getValue();
        }
    }

    return highestValue;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static List<String> keysToList(Map<?, ?> map) {
    List<String> list = new ArrayList<String>();
    Set<?> set = map.entrySet();
    Iterator<?> iter = set.iterator();
    while (iter.hasNext()) {
        Map.Entry m = (Map.Entry) iter.next();
        list.add("" + m.getKey());
    }/*  w w w  .  ja  v a2  s.co  m*/
    return list;
}