基于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);
PS: 欢迎关注公众号"Devin说",会不定期更新Java相关技术知识。