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:cn.com.qiqi.order.utils.Servlets.java

/**
 * ?Parameters?Query StringParameter, paramter nameprefix.
 * //www  . ja v  a2s . c o m
 * @see #getParametersStartingWith
 */
public static String encodeParameterStringWithPrefix(Map<String, Object> params, String prefix) {
    if (params == null || params.size() == 0) {
        return "";
    }

    if (prefix == null) {
        prefix = "";
    }

    StringBuilder queryStringBuilder = new StringBuilder();
    Iterator<Entry<String, Object>> it = params.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, Object> entry = it.next();
        queryStringBuilder.append(prefix).append(entry.getKey()).append('=').append(entry.getValue());
        if (it.hasNext()) {
            queryStringBuilder.append('&');
        }
    }
    return queryStringBuilder.toString();
}

From source file:Main.java

/**
 * Determine if the contents of two maps are the same, i.e. the same keys are mapped to the same values in both maps.
 * @param a the first {@link Map}/*from w  w  w  .jav a2s .com*/
 * @param b the secound {@link Map}
 * @return <code>true</code> if the contents are the same, <code>false</code> otherwise.
 */
public static <K, V> boolean equalContents(Map<? extends K, ? extends V> a, Map<? extends K, ? extends V> b) {
    if (a == b) {
        return true;
    } else if (a.isEmpty() && b.isEmpty()) {
        return true;
    }
    if (a.size() != b.size()) {
        return false;
    } else {

        for (Map.Entry entryA : a.entrySet()) {

            if (!b.containsKey(entryA.getKey()) || !b.get(entryA.getKey()).equals(entryA.getValue())) {
                return false;
            }

        }

        return true;
    }

}

From source file:com.hangum.tadpole.engine.sql.util.export.CSVExpoter.java

/**
 * csv ?? ? ?  ?./*from w  ww.  ja  v a 2s .  c  o  m*/
 * 
 * @param tableName
 * @param rsDAO
 * @param seprator
 * @return ? 
 * 
 * @throws Exception
 */
public static String makeCSVFile(boolean isAddHead, String tableName, QueryExecuteResultDTO rsDAO,
        char seprator) throws Exception {
    String strTmpDir = PublicTadpoleDefine.TEMP_DIR + tableName + System.currentTimeMillis()
            + PublicTadpoleDefine.DIR_SEPARATOR;
    String strFile = tableName + ".csv";
    String strFullPath = strTmpDir + strFile;

    // add bom character
    //       ByteArrayOutputStream out = new ByteArrayOutputStream();
    //       //Add BOM characters
    //       out.write(0xEF);
    //       out.write(0xBB);
    //       out.write(0xBF);
    //       out.write(csvData.getBytes("UTF-8"));

    FileUtils.writeByteArrayToFile(new File(strFullPath),
            (new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }), true);

    List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();
    List<String[]> listCsvData = new ArrayList<String[]>();
    String[] strArrys = null;

    if (isAddHead) {
        // column .
        Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
        strArrys = new String[mapLabelName.size() - 1];
        for (int i = 1; i < mapLabelName.size(); i++) {
            strArrys[i - 1] = mapLabelName.get(i);
        }
        listCsvData.add(strArrys);
        String strTitle = CSVFileUtils.makeData(listCsvData, seprator);
        FileUtils.writeStringToFile(new File(strFullPath), strTitle, true);

        listCsvData.clear();
    }

    // data
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);

        strArrys = new String[mapColumns.size() - 1];
        for (int j = 1; j < mapColumns.size(); j++) {
            strArrys[j - 1] = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j); //$NON-NLS-1$
        }
        listCsvData.add(strArrys);

        if ((i % DATA_COUNT) == 0) {
            FileUtils.writeStringToFile(new File(strFullPath), CSVFileUtils.makeData(listCsvData, seprator),
                    true);
            listCsvData.clear();
        }
    }

    //  ?.
    if (!listCsvData.isEmpty()) {
        FileUtils.writeStringToFile(new File(strFullPath), CSVFileUtils.makeData(listCsvData, seprator), true);
    }

    return strFullPath;
}

From source file:com.eastteam.myprogram.web.Servlets.java

/**
 * ?Parameters?Query StringParameter, paramter nameprefix.
 * //from  www.  j  a  v  a2s.  co m
 * @see #getParametersStartingWith
 */
public static String encodeParameterStringWithPrefix(Map<String, Object> params, String prefix) {
    if ((params == null) || (params.size() == 0)) {
        return "";
    }

    if (prefix == null) {
        prefix = "";
    }

    StringBuilder queryStringBuilder = new StringBuilder();
    Iterator<Entry<String, Object>> it = params.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, Object> entry = it.next();
        if (entry.getValue() instanceof String) {
            queryStringBuilder.append(prefix).append(entry.getKey()).append('=').append(entry.getValue());
        } else {
            String[] myarray = (String[]) entry.getValue();
            for (int i = 0; i < myarray.length; i++) {
                queryStringBuilder.append(prefix).append(entry.getKey()).append('=').append(myarray[i]);
                if (i < myarray.length - 1) {
                    queryStringBuilder.append('&');
                }
            }
        }
        if (it.hasNext()) {
            queryStringBuilder.append('&');
        }
    }
    return queryStringBuilder.toString();
}

From source file:de.avanux.android.livetracker2.HttpUtil.java

public static String post(String url, Map<String, String> httpParameters)
        throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP POST " + url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(httpParameters.size());
    Set<String> httpParameterKeys = httpParameters.keySet();
    for (String httpParameterKey : httpParameterKeys) {
        nameValuePairs.add(new BasicNameValuePair(httpParameterKey, httpParameters.get(httpParameterKey)));
    }//  w w  w  . ja v a 2  s. c  o  m

    HttpPost method = new HttpPost(url);
    method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
}

From source file:Main.java

public static Map<? extends Object, ? extends Double> merge(Map<? extends Object, ? extends Double> map1,
        Map<? extends Object, ? extends Double> map2) {
    Map<Object, Double> result = new HashMap<Object, Double>();

    Map<? extends Object, ? extends Double> biggerMap = map1.size() > map2.size() ? map1 : map2;
    Map<? extends Object, ? extends Double> smallerMap = map1.size() > map2.size() ? map2 : map1;

    for (Entry<? extends Object, ? extends Double> entry : smallerMap.entrySet()) {
        Object key = entry.getKey();
        Double value = entry.getValue();

        Double value2 = (Double) getValue(biggerMap, key, Double.class);
        value2 += value;// w w w.  ja va  2s.co  m
        result.put(key, value2);
        biggerMap.remove(key);
    }
    result.putAll(biggerMap);
    return sortByValueDesc(result);
}

From source file:com.act.analysis.similarity.SimilarityAnalysis.java

public static Map<String, String> doubleMapToStringMap(Map<String, Double> m) {
    Map<String, String> r = new HashMap<>(m.size());
    for (Map.Entry<String, Double> entry : m.entrySet()) {
        r.put(entry.getKey(), String.format("%.6f", entry.getValue()));
    }/*w w w  . j av a2s .c o  m*/
    return r;
}

From source file:com.cnksi.core.web.utils.Servlets.java

/**
 * ?Parameters?Query StringParameter, paramter nameprefix.
 * //  w  w  w . ja v  a  2 s.co  m
 * @see #getParametersStartingWith
 */
public static String encodeParameterStringWithPrefix(Map<String, Object> params, String prefix) {

    if (params == null || params.size() == 0) {
        return "";
    }

    if (prefix == null) {
        prefix = "";
    }

    StringBuilder queryStringBuilder = new StringBuilder();
    Iterator<Entry<String, Object>> it = params.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, Object> entry = it.next();
        queryStringBuilder.append(prefix).append(entry.getKey()).append('=').append(entry.getValue());
        if (it.hasNext()) {
            queryStringBuilder.append('&');
        }
    }
    return queryStringBuilder.toString();
}

From source file:com.github.lpezet.antiope.util.HttpUtils.java

public static String encodeParameters(Map<String, String> pParameters) {
    List<NameValuePair> nameValuePairs = null;
    if (pParameters.size() > 0) {
        nameValuePairs = new ArrayList<NameValuePair>(pParameters.size());
        for (Entry<String, String> entry : pParameters.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }//from  w  w  w. jav a  2 s .  c  om
    }

    String encodedParams = null;
    if (nameValuePairs != null) {
        encodedParams = URLEncodedUtils.format(nameValuePairs, DEFAULT_ENCODING);
    }

    return encodedParams;
}

From source file:ch.ralscha.extdirectspring_itest.RawJsonControllerTest.java

@SuppressWarnings("unchecked")
private static void testAndCheck(String action, String method, Integer total, boolean success)
        throws IOException, JsonParseException, JsonMappingException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {//www. ja va2s. c  om
        HttpPost post = new HttpPost("http://localhost:9998/controller/router");

        StringEntity postEntity = new StringEntity("{\"action\":\"" + action + "\",\"method\":\"" + method
                + "\",\"data\":[],\"type\":\"rpc\",\"tid\":1}", "UTF-8");
        post.setEntity(postEntity);
        post.setHeader("Content-Type", "application/json; charset=UTF-8");

        response = client.execute(post);
        HttpEntity entity = response.getEntity();
        assertThat(entity).isNotNull();
        String responseString = EntityUtils.toString(entity);

        assertThat(responseString).isNotNull();
        assertThat(responseString).startsWith("[").endsWith("]");

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> rootAsMap = mapper
                .readValue(responseString.substring(1, responseString.length() - 1), Map.class);
        assertEquals(5, rootAsMap.size());

        assertEquals(method, rootAsMap.get("method"));
        assertEquals("rpc", rootAsMap.get("type"));
        assertEquals(action, rootAsMap.get("action"));
        assertEquals(1, rootAsMap.get("tid"));

        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        if (total != null) {
            assertEquals(3, result.size());
            assertThat((Integer) result.get("total")).isEqualTo(total);
        } else {
            assertEquals(2, result.size());
        }
        assertThat((Boolean) result.get("success")).isEqualTo(success);

        List<Map<String, Object>> records = (List<Map<String, Object>>) result.get("records");
        assertEquals(2, records.size());

        assertEquals("4cf8e5b8924e23349fb99454", ((Map<String, Object>) records.get(0).get("_id")).get("$oid"));
        assertEquals("4cf8e5b8924e2334a0b99454", ((Map<String, Object>) records.get(1).get("_id")).get("$oid"));
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}