Example usage for java.util Map size

List of usage examples for java.util Map size

Introduction

In this page you can find the example usage for java.util Map size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:Main.java

public static Collection<Object> getSortedValues(Map<Integer, Object> vParameters) {
    if (vParameters == null || vParameters.size() == 0)
        return null;
    Set<Integer> keys = vParameters.keySet();

    SortedMap<Integer, Object> sortedMap = new TreeMap<Integer, Object>(vParameters);
    Collection<Object> result = sortedMap.values();
    return result;
}

From source file:Main.java

public static List<NameValuePair> convertParams(Map<String, String> params) {
    if (params == null || params.size() == 0)
        return null;

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    Set<Entry<String, String>> setEntry = params.entrySet();
    BasicNameValuePair bnvp = null;//from w w w  .  j a v  a  2 s  .c  om

    for (Entry<String, String> entry : setEntry) {
        bnvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
        nameValuePairs.add(bnvp);
    }

    return nameValuePairs;
}

From source file:Main.java

public static boolean isEmpty(Map<?, ?> values) {
    return values == null || values.size() == 0;
}

From source file:Main.java

/**
 * Return true if this map is null or it's empty.
 * //from   ww w.  java 2  s . co  m
 * @param map
 * @return
 */
public static boolean isEmpty(Map<? extends Object, ? extends Object> map) {
    return map == null || map.size() == 0;
}

From source file:Main.java

/**
 * json//from ww w  . j  av  a  2  s . c o m
 * @param params
 * @return
 */
public static String buildJsonString(Map<String, Object> params) {
    if (params == null || params.size() == 0)
        return null;

    Set<Entry<String, Object>> setEntry = params.entrySet();
    StringBuilder str = new StringBuilder();
    str.append("{");
    for (Entry<String, Object> entry : setEntry) {
        str.append("\"");
        str.append(entry.getKey());
        str.append("\"");
        str.append(":");
        if (entry.getValue() instanceof String) {
            str.append("\"");
        }
        str.append(entry.getValue());
        if (entry.getValue() instanceof String) {
            str.append("\"");
        }
        str.append(",");
    }
    str.setCharAt(str.length() - 1, '}');
    return str.toString();
}

From source file:Main.java

public static int getLength(Map<?, ?> map) {
    if (map == null) {
        return 0;
    }/*w  ww.  j  a  v a 2  s.co m*/

    return map.size();
}

From source file:Main.java

/**
 * Reverses a map (switches key and value types).
 *
 * @param <K> the key type/*from www  .  j  a va  2 s.c o  m*/
 * @param <V> the value type
 * @param map the map
 *
 * @return the reversed map
 */
public static <K, V> Map<V, K> reverse(final Map<K, V> map) {
    final Map<V, K> reversed = new HashMap<V, K>(map.size());
    for (final Entry<K, V> e : map.entrySet()) {
        reversed.put(e.getValue(), e.getKey());
    }
    return reversed;
}

From source file:Main.java

/**
 * join paras/*w  w  w.  jav a 2s  .  com*/
 *
 * @param parasMap paras map, key is para name, value is para value
 * @return join key and value with {@link #EQUAL_SIGN}, join keys with {@link #PARAMETERS_SEPARATOR}
 */
public static String joinParas(Map<String, String> parasMap) {
    if (parasMap == null || parasMap.size() == 0) {
        return null;
    }

    StringBuilder paras = new StringBuilder();
    Iterator<Map.Entry<String, String>> ite = parasMap.entrySet().iterator();
    while (ite.hasNext()) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) ite.next();
        paras.append(entry.getKey()).append(EQUAL_SIGN).append(entry.getValue());
        if (ite.hasNext()) {
            paras.append(PARAMETERS_SEPARATOR);
        }
    }
    return paras.toString();
}

From source file:Main.java

/**
 * Nullsafe method returning the number of entries the incoming map contains.
 *
 * @param m Map of Map.Entry objects to count the number of entries from
 * @return int containing the number of the entries in the map, or -1 if the map is null
 *//*from w  w w . ja va  2 s  .  co m*/
public static int size(Map m) {
    int size = -1;

    if (m != null) {
        size = m.size();
    }

    return size;
}

From source file:Main.java

/**
 * @param map map//ww w.  ja v  a 2 s  .c o m
 * @return map deep copy
 */
public static Map<String, Set<String>> cloneMap(Map<String, Set<String>> map) {
    final Map<String, Set<String>> clone = new HashMap<>(map.size());
    for (Map.Entry<String, Set<String>> entry : map.entrySet()) {
        final Set<String> cloneSet = new HashSet<>(entry.getValue());
        clone.put(entry.getKey(), cloneSet);
    }

    return clone;
}