import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * @Author: hoje * Description: * Date: Created in 10:05 2019/12/6 0006 * Modified by: */ public class Map2Equal { public static void main(String[] args) { Map<String, String> data = new HashMap<>(); data.put("aa", "11"); data.put("bb", "22"); data.put("cc", "33"); StringBuffer retStr = new StringBuffer("https://www.baidu.com"); StringBuffer sb = new StringBuffer(); retStr.append("?"); if (data != null) { Iterator i = data.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, String> entry = (Map.Entry) i.next(); sb.append("&").append((String) entry.getKey()).append("=").append((String) entry.getValue()); } String param = sb.substring(1); retStr.append(param); } System.out.println(retStr); } }
测试结果:
https://www.baidu.com?aa=11&bb=22&cc=33