黑马程序员技术交流社区

标题: HashMap泛型编程 [打印本页]

作者: AI_96    时间: 2015-8-2 14:02
标题: HashMap泛型编程
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)




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2