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 List<Map.Entry<String, Double>> sortMapByValue(Map<String, Double> map) {
    List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
        @Override/*  w  ww .j  a v  a  2  s.  com*/
        public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
            double result = o2.getValue() - o1.getValue();
            if (result > 0)
                return -1;
            else if (result == 0)
                return 0;
            else
                return 1;
        }
    });
    return list;
}

From source file:Main.java

public static String serializerMap(Map<String, String> map) {
    StringBuilder builder = new StringBuilder();

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String val = entry.getValue();
        builder.append(key + ENTRY_DELIMITER + val);
        builder.append(DELIMITER);/*from w  w  w  .ja  v a2  s  . co  m*/
    }
    return builder.toString();
}

From source file:Main.java

public static <K, V> K getFirstKey(Map<K, V> map, V value) {
    K ret = null;//from  w w  w.j a v  a 2s  .c om
    if (map != null && value != null) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (value.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Creates a list of sorted entries by copying them from the entrySet of a
 * map.//from w  ww  .  j av a 2 s.  c  om
 * 
 * @param map
 *            the source map
 * @param comparator
 *            the desired order for the entries list
 * 
 * @return a newly created sorted list
 */
public static <K, V> List<Entry<K, V>> order(Map<K, V> map, java.util.Comparator<Entry<K, V>> comparator) {
    return Ordering.from(comparator).sortedCopy(map.entrySet());
}

From source file:Main.java

public static <K, V> Map<V, K> reverseMap(Map<K, V> map) {
    if (map != null) {
        Map<V, K> retMap = new HashMap<V, K>();
        for (Entry<K, V> entry : map.entrySet()) {
            retMap.put(entry.getValue(), entry.getKey());
        }/* w  ww.jav a2 s.co  m*/
        return retMap;
    }
    return null;
}

From source file:Main.java

public static <E, V extends Comparable<? super V>, K> Map<K, V> sortByValueAsc(Map<K, V> map, int limit) {
    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 (o1.getValue()).compareTo(o2.getValue());
        }//  www  . java  2s  .  co m
    });

    Map<K, V> result = new LinkedHashMap<K, V>();
    int counter = 0;
    for (Map.Entry<K, V> entry : list) {
        if (limit > 0 && counter >= limit) {
            break;
        }
        result.put(entry.getKey(), entry.getValue());
        counter++;
    }
    return result;
}

From source file:Main.java

public static <K> Map<K, Long> convertMapValueToLong(Map<K, String> map) {
    Map<K, Long> covertedResult = new HashMap<K, Long>(map.size());

    Set<Entry<K, String>> entrySet = map.entrySet();
    for (Entry<K, String> entry : entrySet) {
        covertedResult.put(entry.getKey(), Long.valueOf(entry.getValue().trim()));
    }/*  www.  j  a v a2 s. c o m*/
    return covertedResult;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String MapToString(Map<String, String> map) {
    Entry entry;//from w  w w  . jav  a2 s  .c o m
    StringBuffer sb = new StringBuffer();
    for (Iterator<?> iterator = map.entrySet().iterator(); iterator.hasNext();) {
        entry = (Entry) iterator.next();
        sb.append(entry.getKey().toString()).append("'")
                .append(null == entry.getValue() ? "" : entry.getValue().toString())
                .append(iterator.hasNext() ? "^" : "");
    }
    return sb.toString();
}

From source file:Main.java

public static String printStrsMap(Map<String, String[]> map) {
    StringBuilder stringBuilder = new StringBuilder();

    for (Map.Entry<String, String[]> entry : map.entrySet()) {
        stringBuilder.append(entry.getKey()).append("=").append(Arrays.toString(entry.getValue()))
                .append("\r\n");
    }/*  ww  w . j a v a  2s  . c o  m*/

    return stringBuilder.toString();
}

From source file:com.axibase.tsd.plain.AbstractInsertCommand.java

protected static void appendKeysAndValues(StringBuilder sb, String prefix, Map<String, String> map) {
    for (Map.Entry<String, String> tagNameAndValue : map.entrySet()) {
        sb.append(prefix).append(clean(tagNameAndValue.getKey())).append('=')
                .append(normalize(tagNameAndValue.getValue()));
    }/*www .java2 s  .c o m*/
}