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

/**
 * Get key value of map in String/* w  ww. j  a  va2 s.  c o  m*/
 * 
 * @version 1.0
 * @author pankajbharti
 */
@SuppressWarnings("rawtypes")
public static String getKeyValue(Map map) {
    StringBuilder str = new StringBuilder("");

    Set keySet = map.entrySet();

    Iterator itr = keySet.iterator();

    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();

        str.append(entry.getKey() + " : " + entry.getValue() + "\n" + "\t \t \t \t \t");
    }

    return str.toString();
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.clustering.entropy.MatrixExperiments.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<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>>() {
        @Override/*from www  .  j a v  a2s.  c o  m*/
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }
    });

    LinkedHashMap<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

/**
 * Get the total number of elements in a map whose values are lists of elements.
 * @param <T> the type of the keys in the map.
 * @param <U> the type of the elements in the lists.
 * @param map the map of which to get the size.
 * @return the size of the map as an int value.
 *///  ww w .j  a va 2  s.  c  om
public static <T, U> int sizeOfListMap(Map<T, List<U>> map) {
    int result = 0;
    for (Map.Entry<T, List<U>> entry : map.entrySet())
        result += entry.getValue().size();
    return result;
}

From source file:Main.java

public static void saveData(SharedPreferences sp, Map<String, String> datas) {
    Editor editor = sp.edit();//www .j  a  v  a2  s .  c  o m
    for (Map.Entry<String, String> entry : datas.entrySet()) {
        editor.putString(entry.getKey(), entry.getValue());
    }
    editor.commit();
}

From source file:Main.java

static Map hoodieObjectFromCouchObject(Map couchObject) {

    Map result = new HashMap();

    Iterator it = couchObject.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();

        String key = pair.getKey().toString();
        String value = pair.getValue().toString();

        if (key.equals("_id")) {

            result.put("id", value.split("/")[1]);
            result.put("type", value.split("/")[0]);

        } else {//from   w w  w .j  a v  a  2 s.c  o m
            result.put(pair.getKey(), pair.getValue());
        }
    }

    return result;
}

From source file:Main.java

/**
 * Many to One way of finding Keys from value
 * @param map//from w  w w. j  av  a  2  s .  c  o  m
 * @param value
 * @return keys
 */
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<T>();
    for (Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            keys.add(entry.getKey());
        }
    }
    return keys;
}

From source file:Main.java

public static String constructQueryString(final Map<String, String> query) {
    final Set<Map.Entry<String, String>> mapEntries = query.entrySet();
    final Iterator<Map.Entry<String, String>> iterator = mapEntries.iterator();
    final StringBuilder queryStringBuilder = new StringBuilder();

    while (iterator.hasNext()) {
        final Map.Entry<String, String> entry = iterator.next();
        final String pair = String.format("%s=%s", entry.getKey(), entry.getValue());
        queryStringBuilder.append(pair);
        if (iterator.hasNext()) {
            queryStringBuilder.append('&');
        }/*from w  w  w. j  a  v a  2  s . c o  m*/

    }
    return queryStringBuilder.toString();
}

From source file:Main.java

public static <K, V> String mapToString(Map<K, V> map, String seperator) {
    StringBuffer sb = new StringBuffer();
    for (Entry<K, V> entry : map.entrySet()) {
        sb.append(entry.getKey() + seperator + entry.getValue() + "\n");
    }/*from   w ww . j a  v a2s .  c  o  m*/
    return sb.toString();
}

From source file:Main.java

public static Map<String, String> grabProperty(Map<String, String> map, Element elem) {
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String val = "";
        NodeList nodeList = elem.getElementsByTagName(key);
        if (nodeList == null || nodeList.getLength() <= 0) {
            //entry.setValue("");
        } else {/*from   w  w w. j a  v  a 2s .  co  m*/
            Node node = nodeList.item(0);
            if (node != null)
                node = node.getFirstChild();
            if (node != null)
                val = node.getNodeValue();
            //String val=nodeList.item(0).getFirstChild().getNodeValue();
            //entry.setValue("");
        }
        entry.setValue(val);

    }
    return map;
}

From source file:Main.java

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());/* www  . j a v  a2  s  .  c  om*/
        }
    }
    return keys;
}