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

/**
 * Copies the given {@link Map} containing another {@link Map} into a new
 * {@link Map}.//from w ww  . j  av  a2s  .co  m
 * 
 * @param <A>
 *            the type of the keys of the outer map
 * @param <B>
 *            the type of the keys of the map, that is the value of the
 *            outer map
 * @param <C>
 *            the type of the values of the map, that is the value of the
 *            outer map
 * @param data
 *            the given map
 * @return If the given map was empty, a {@link Collections#emptyMap()} is
 *         returned<br>
 *         If the given map contained only one entry, a
 *         {@link Collections#singletonMap(Object, Object)}, containing
 *         said entry, is returned <br>
 *         If the given map contained more than one element, a
 *         {@link Collections#unmodifiableMap(Map)}, containing the entries
 *         of the given map, is returned.
 */
public static <A, B, C> Map<A, Map<B, C>> copyDeep(Map<A, Map<B, C>> data) {
    final int size = data.size();

    switch (size) {
    case 0:
        return Collections.emptyMap();
    case 1:
        final A key = data.keySet().iterator().next();
        return Collections.singletonMap(key, copy(data.get(key)));
    default:
        final Map<A, Map<B, C>> newData = new HashMap<A, Map<B, C>>();
        for (Map.Entry<A, Map<B, C>> entry : data.entrySet()) {
            newData.put(entry.getKey(), copy(entry.getValue()));
        }
        return Collections.unmodifiableMap(newData);
    }
}

From source file:Main.java

public static boolean mapEquals(Map map1, Map map2) {
    if (map1 == null && map2 == null)
        return true;
    if (map1 == null || map2 == null)
        return false;
    if (map1.size() != map2.size())
        return false;
    for (Iterator i$ = map1.entrySet().iterator(); i$.hasNext();) {
        java.util.Map.Entry entry = (java.util.Map.Entry) i$.next();
        Object key = entry.getKey();
        Object value1 = entry.getValue();
        Object value2 = map2.get(key);
        if (!objectEquals(value1, value2))
            return false;
    }//  w w w . j av a  2  s .c  om

    return true;
}

From source file:costumetrade.common.util.HttpClientUtils.java

/**
 * @param url//ww  w  .  ja  v  a  2 s.c om
 * @param ?
 * @return
 */
public static String doPost(String url, Map<String, Object> params) {
    List<BasicNameValuePair> nvps = new ArrayList<>(params.size());
    for (String key : params.keySet()) {
        Object value = params.get(key);
        if (value != null) {
            nvps.add(new BasicNameValuePair(key, value.toString()));
        }
    }
    return doPost(url, new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
}

From source file:com.vk.sdk.util.VKStringJoiner.java

/**
 * Join parameters map into string, usually query string
 *
 * @param queryParams Map to join//from   w w w  . j a  va2  s. c o  m
 * @param isUri       Indicates that value parameters must be uri-encoded
 * @return Result query string, like k=v&k1=v1
 */
public static String joinParams(Map<String, Object> queryParams, boolean isUri) {
    ArrayList<String> params = new ArrayList<String>(queryParams.size());
    for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof VKAttachments) {
            value = ((VKAttachments) value).toAttachmentsString();
        }
        params.add(String.format("%s=%s", entry.getKey(),
                isUri ? Uri.encode(String.valueOf(value)) : String.valueOf(value)));
    }
    return join(params, "&");
}

From source file:com.cubeia.backoffice.report.RequestBean.java

@SuppressWarnings("rawtypes")
private static Map<String, String> checkParameters(Map p) {
    Map<String, String> m = new HashMap<String, String>(p.size());
    for (Object o : p.keySet()) {
        String key = o.toString();
        if (key.startsWith("param:")) {
            key = key.substring(6);//from w  ww.  j a  v a 2s .  c om
            Object v = p.get(o);
            m.put(key, v.toString());
        }
    }
    return m;
}

From source file:com.egt.core.db.ddl.Writer.java

private static void execute(String vm, Map clases) {
    //      Utils.println("*** combinar(" + vm + ", clases=" + clases.size() + ") ***");
    if (clases == null || clases.size() == 0) {
        return;/*w ww .  j  a va2s  . co m*/
    }
    try {
        Template template = Velocity.getTemplate(vm);
        VelocityContext context = new VelocityContext();
        context.put("clases", clases);
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        String filename = vm.replace(".vm", ".sql");
        FileWriter fileWriter = new FileWriter(filename);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(sw.toString());
        bufferedWriter.close();
    } catch (ResourceNotFoundException ex) {
        ex.printStackTrace();
    } catch (ParseErrorException ex) {
        ex.printStackTrace();
    } catch (MethodInvocationException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static <K, V> List<Integer> getIndexListByValues(Map<K, V> map, Object[] values) {
    if ((values == null) || (values.length == 0)) {
        return null;
    }//from   ww w .  j  a  v  a 2s  .  c  o  m
    List<Integer> indexList = new ArrayList<Integer>(map.size());
    Integer i = Integer.valueOf(0);
    for (V v : map.values()) {
        for (Object value : values) {
            if ((v == value) || (v.equals(value))) {
                indexList.add(i);
            }
        }
        i = Integer.valueOf(i.intValue() + 1);
    }
    return indexList;
}

From source file:com.linkedin.flashback.http.HttpUtilities.java

/**
 * Converts an ordered map of key / value pairs to a URL parameter string
 * @param params the map of key / value pairs
 * @return a string representation of the key / value pairs, delimited by '&'
 *//*from  w w  w  .  j av  a  2 s  . c om*/
static public String urlParamsToString(Map<String, String> params, String charset)
        throws UnsupportedEncodingException {
    if (params.size() == 0) {
        return "";
    }
    StringBuilder resultBuilder = new StringBuilder();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        String encodedName = URLEncoder.encode(entry.getKey(), charset);
        String encodedValue = URLEncoder.encode(entry.getValue(), charset);
        resultBuilder.append(encodedName).append("=").append(encodedValue).append("&");
    }
    resultBuilder.deleteCharAt(resultBuilder.length() - 1);
    return resultBuilder.toString();
}

From source file:Main.java

/**
 * This method returns the prefix associated with the supplied namespace.
 * /*  ww  w .j  av  a2s .  c  om*/
 * @param namespace The namespace
 * @param nsMap The existing namespace prefix mappings
 * @return The prefix
 */
public static String getPrefixForNamespace(String namespace, java.util.Map<String, String> nsMap) {
    String prefix = null;

    prefix = nsMap.get(namespace);

    if (prefix == null) {
        prefix = NS_LABEL + (nsMap.size() + 1);
        nsMap.put(namespace, prefix);
    }

    return (prefix);
}

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 *//* w  w  w.  ja  va2 s .  c om*/
private static <K, V> Map.Entry<K, V> getRandomElement(final boolean remove, final Random random,
        final Map<K, V> map) {
    if (map.isEmpty())
        throw new IllegalArgumentException("map is empty!");
    final int index = random.nextInt(map.size());

    final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
    int i = 0;

    while (i++ < index)
        it.next();

    final Map.Entry<K, V> elem = it.next();
    if (remove)
        it.remove();
    return elem;
}