HashMap泛型编程

  基于Java HashMap的泛型编程,常用的代码形式如下(开发中遇到的一个Case):

复制代码
        //HashMap的创建和值添加
     HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("appName", "IPAD APP");
        hashMap.put("appVer", "2.0");
        hashMap.put("cmd","CV_Services.GetIndustryNews('consumer discretionary','en',0,10,'','XSHG,XSHE')");
        hashMap.put("alg", "alg");
        hashMap.put("appID", "device02154698");

     //泛型HashMap的遍历
        StringBuffer urlBuffer = new StringBuffer();
        Iterator<Entry<String, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> entry = iterator.next();
            urlBuffer.append(entry.getKey());
            urlBuffer.append("&");
            urlBuffer.append(entry.getValue());
            urlBuffer.append("&;");
        }
        
        String url="";
        try {
            url=URLEncoder.encode(urlBuffer.toString(), "UTF-8");  //URL编码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        System.out.println(url);
复制代码

你可能感兴趣的:(java,泛型,HashMap,遍历,StringBuffer)