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> List<K> getKeyListByValues(Map<K, V> map, V[] values) {
    if ((values == null) || (values.length == 0)) {
        return null;
    }//from   w w w  .  j  a  v  a  2  s.  co  m
    List<K> keyList = new ArrayList<K>(map.size());
    Set<Map.Entry<K, V>> entrySet = map.entrySet();
    for (Iterator<?> localIterator = entrySet.iterator(); localIterator.hasNext();) {
        Map.Entry<K, V> entry = (Map.Entry) localIterator.next();

        for (int i = 0; i < values.length; i++) {
            Object value = values[i];
            if ((entry.getValue() == null)
                    || ((entry.getValue() != null) && (entry.getValue().equals(value)))) {
                keyList.add(entry.getKey());
            }
        }
    }
    return keyList;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean decesnding) {
    int s2 = 1;/*  www. j  a v  a2s  . c o m*/
    if (decesnding)
        s2 = -1;
    final int sign = s2;

    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());

    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {

        @Override
        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            return sign * o1.getValue().compareTo(o2.getValue());
        }
    });

    Map<K, V> sorted = new LinkedHashMap<>();
    for (Map.Entry<K, V> entry : list) {
        sorted.put(entry.getKey(), entry.getValue());
    }

    return sorted;
}

From source file:de.metas.ui.web.window.datatypes.json.JSONLookupValue.java

public static final IntegerLookupValue integerLookupValueFromJsonMap(final Map<String, String> map) {
    final Set<Map.Entry<String, String>> entrySet = map.entrySet();
    if (entrySet.size() != 1) {
        throw new IllegalArgumentException("Invalid JSON lookup value: map=" + map);
    }/*from  w w  w.  j ava  2  s . c  om*/
    final Map.Entry<String, String> e = entrySet.iterator().next();

    String idStr = e.getKey();
    if (idStr == null) {
        return null;
    }
    idStr = idStr.trim();
    if (idStr.isEmpty()) {
        return null;
    }

    final int id = Integer.parseInt(idStr);
    final String name = e.getValue();

    return IntegerLookupValue.of(id, name);
}

From source file:de.tudarmstadt.ukp.dkpro.tc.crfsuite.writer.CRFSuiteDataWriter.java

public static String outcomeMap2String(Map<String, Integer> map) {
    StringBuilder sb = new StringBuilder();
    for (Entry<String, Integer> entry : map.entrySet()) {
        sb.append(entry.getKey());/*from  ww w  . j  a  va2  s. c  o m*/
        sb.append("\t");
        sb.append(entry.getValue());
        sb.append("\n");
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Utility method for building up URL param string.
 * //from  ww  w.  ja  v  a 2s .c  o m
 * @param params
 * @return URL parameter string
 */
private static String buildParamString(Map<String, String> params) {
    StringBuffer paramBuf = new StringBuffer();
    if (params != null && params.size() > 0) {
        paramBuf.append('?');
        Iterator<Map.Entry<String, String>> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, String> param = (Map.Entry<String, String>) iter.next();
            paramBuf.append(param.getKey() + "=" + param.getValue());
            if (iter.hasNext()) {
                paramBuf.append('&');
            }
        }
    }
    Log.i("PARAM STRING CHECK", paramBuf.toString());
    return paramBuf.toString();
}

From source file:com.zxy.commons.lang.utils.ObjectsUtils.java

/**
 * ??/*from   ww  w .j  ava 2s .c  o  m*/
 * 
 * @param obj 
 * @param toStringStyle ?
 *                      {@code ToStringStyle.SIMPLE_STYLE }
 *                      {@code ToStringStyle.SHORT_PREFIX_STYLE }
 *                      {@code ToStringStyle.MULTI_LINE_STYLE }
 * @return ??
 * @see ToStringStyle
 */
public static String toString(Object obj, ToStringStyle toStringStyle) {
    if (obj == null) {
        return null;
    }
    if ((obj instanceof Collection)) {
        Collection<?> cs = (Collection<?>) obj;
        List<String> returnCs = new ArrayList<>();
        for (Object c : cs) {
            returnCs.add(ReflectionToStringBuilder.toString(c, toStringStyle));
        }
        return returnCs.toString();
    }
    if ((obj instanceof Map)) {
        Map<?, ?> maps = (Map<?, ?>) obj;
        Map<String, String> returnMaps = new HashMap<>();
        for (Map.Entry<?, ?> entry : maps.entrySet()) {
            String key = entry.getKey().toString();
            String value = ReflectionToStringBuilder.toString(entry.getValue(), toStringStyle);
            returnMaps.put(key, value);
        }
        return returnMaps.toString();
    }
    //        return ReflectionToStringBuilder.toString(obj, ToStringStyle.SHORT_PREFIX_STYLE);
    return ReflectionToStringBuilder.toString(obj, toStringStyle);
}

From source file:net.sephy.postman.util.PostmanUtils.java

/**
 *  Map ??? List<Header> ?//  w w  w.  j  ava 2  s  .  c o  m
 * @param map
 * @return
 */
public static List<Header> mapToHeaderList(Map<String, Object> map) {
    List<Header> list = new ArrayList<Header>();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        Object value = entry.getValue();
        if (value != null) {
            list.add(new BasicHeader(entry.getKey(), value.toString()));
        }
    }
    return list;
}

From source file:com.feilong.taglib.display.option.OptionBuilder.java

/**
 * Builds the content main./*from   w w  w .  j a  v  a2  s  .  com*/
 *
 * @param optionParam
 *            the option param
 * @return the string
 */
private static String buildContentMain(OptionParam optionParam) {
    StringBuilder sb = new StringBuilder();

    // key value map.
    Map<String, String> map = toMap(getResourceBundle(optionParam.getBaseName(), optionParam.getLocale()));
    for (Map.Entry<String, String> entry : map.entrySet()) {
        String option = buildOption(entry.getKey(), entry.getValue(), optionParam.getSelectedKey());
        sb.append(option).append(LINE_SEPARATOR);
    }
    return sb.toString();
}

From source file:ductive.stats.StatsUtils.java

private static void postprocess(Map<String, Object> tree, String[] path, Map<String, Object> result) {
    for (Entry<String, Object> e : tree.entrySet()) {
        Object val = e.getValue();
        if (Map.class.isInstance(val)) {
            postprocess(mapCast(e.getValue()), ArrayUtils.add(path, e.getKey()), result);
        } else {//from   www .  ja  v a 2 s.c  om
            result.put(StringUtils.join(ArrayUtils.add(path, e.getKey()), '.'), val);
        }
    }
}

From source file:Main.java

public final static <K, V> String join(Map<K, V> map, String separator, String separator1) {
    if (map == null || map.size() == 0) {
        return "";
    }/*from ww w. ja  va2 s  .  co  m*/
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<K, V> entry : map.entrySet()) {
        sb.append(String.valueOf(entry.getKey())).append(separator1).append(String.valueOf(entry.getValue()))
                .append(separator);
    }
    return sb.toString().substring(0, sb.toString().length() - separator.length());
}