基於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相關技術知識。
