合并两个map:
eg1.
public static void main(String args[])
{
Map
Map
map.put("1",new Integer(1));
map.put("2", new Integer(2));
map1.put("3", new Integer(3));
map1.put("4", new Integer(4));
map.putAll(map1);
System.out.println(map1);
System.out.println(map);
}
结果是:
{3=3, 4=4}
{3=3, 2=2, 1=1, 4=4}
eg2
HashMap map=new HashMap();
map.put("1", "A");
HashMap map1 = new HashMap();
map1.put("2", "B");
map1.put("3", "C");
map.putAll(map1);
System.out.println(map);
打印结果:
{3=C, 2=B, 1=A}
如果是如下:
HashMap map=new HashMap();
map.put("1", "A");
HashMap map1 = new HashMap();
map1.put("2", "B");
map1.put("1", "C");
map.putAll(map1);
System.out.println(map);
打印结果:
{2=B, 1=C}
所以说用putAll就可以合并两个MAP,只不过如果有相同的key那么用后面的覆盖前面的
像下面水位这样的表,循环一排显示两个数据的:原理还是一排显示一个数据,”只不过这个数据里有两个数据“
controller里:
//全部测站点实时数据
@RequestMapping(value = "thereal")
public ModelAndView thereal(
@RequestParam(value = "stcd", required = false, defaultValue = "") String stcd,
@RequestParam(value = "pageIndex", required = false, defaultValue = "0") Integer pageIndex,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String urlwater="http://192.168.1.15:8080/pingshan-service/data/Query1?sttp=ZZ";
String datawater=result(urlwater);
int indexdaowater=datawater.indexOf("{");
String resultdaowater=datawater.substring(indexdaowater);
String resultdaowater2=resultdaowater.replace(")","");
// 得到一个json对象
JSONObject jsObjwater = JSONObject.fromObject(resultdaowater2);
JSONArray jsonArraywater = jsObjwater.getJSONArray("data");
List
jsp页面上:
${item.stnm1}(${item.stcd1}) | ${item.tm1} | ${item.z1} | //第一排第一列${item.stnm2}(${item.stcd2}) | ${item.tm2} | ${item.z2} | //第一排第二列
String urlwater="http://192.168.1.15:8080/pingshan-service/data/Query1?sttp=ZZ";
String datawater=result(urlwater);
int indexdaowater=datawater.indexOf("{");
String resultdaowater=datawater.substring(indexdaowater);
String resultdaowater2=resultdaowater.replace(")","");
// 得到一个json对象
JSONObject jsObjwater = JSONObject.fromObject(resultdaowater2);
JSONArray jsonArraywater = jsObjwater.getJSONArray("data");
List
List
List
List
//拆分json并把它分为两个list
for(int i=0;i
JSONObject jobj = (JSONObject) jsonArraywater.get(i);
Map mapwater3 = new HashMap();
if(i%2==0){//把奇数个归为一类
Map mapwater11 = new HashMap();
String stcd1 = (String) jobj.get("stcd");
String tm1 = (String) jobj.get("tm");
Double z1 = (Double) jobj.get("z");
String stnm1 = (String) jobj.get("stnm");
mapwater11.put("stcd1", stcd1);
mapwater11.put("tm1", tm1);
mapwater11.put("z1", z1);
mapwater11.put("stnm1", stnm1);
listwater1.add(mapwater11);
}else {//把偶数个归为一类
Map mapwater21 = new HashMap();
String stcd2 = (String) jobj.get("stcd");System.out.println(i+"stcd2:"+stcd2);
String tm2 = (String) jobj.get("tm");
Double z2 = (Double) jobj.get("z");
String stnm2 = (String) jobj.get("stnm");
mapwater21.put("stcd2", stcd2);
mapwater21.put("tm2", tm2);
mapwater21.put("z2", z2);
mapwater21.put("stnm2", stnm2);
listwater2.add(mapwater21);
}
}
//判断奇偶,把两个list通过map合并为一个list
if (jsonArraywater.size() % 2 == 0) {//为偶数时
for (int j = 0; j < jsonArraywater.size() / 2; j++) {
Map mapwater = new HashMap();
mapwater.put("stcd1", listwater1.get(j).get("stcd1"));
mapwater.put("tm1", listwater1.get(j).get("tm1"));
mapwater.put("z1", listwater1.get(j).get("z1"));
mapwater.put("stnm1", listwater1.get(j).get("stnm1"));
mapwater.put("stcd2", listwater2.get(j).get("stcd2"));
mapwater.put("tm2", listwater2.get(j).get("tm2"));
mapwater.put("z2", listwater2.get(j).get("z2"));
mapwater.put("stnm2", listwater2.get(j).get("stnm2"));
listwater.add(mapwater);
}
} else {
for (int j = 0; j < (jsonArraywater.size() + 1) / 2; j++) {
if (j < (jsonArraywater.size() - 1) / 2) {//为奇数全部有数据时
Map mapwater = new HashMap();
mapwater.put("stcd1", listwater1.get(j).get("stcd1"));
mapwater.put("tm1", listwater1.get(j).get("tm1"));
mapwater.put("z1", listwater1.get(j).get("z1"));
mapwater.put("stnm1", listwater1.get(j).get("stnm1"));
mapwater.put("stcd2", listwater2.get(j).get("stcd2"));
mapwater.put("tm2", listwater2.get(j).get("tm2"));
mapwater.put("z2", listwater2.get(j).get("z2"));
mapwater.put("stnm2", listwater2.get(j).get("stnm2"));
listwater.add(mapwater);
} else {//最后一对,一个有数据,一个没数据
Map mapwater = new HashMap();
mapwater.put("stcd1", "");
mapwater.put("tm1", "");
mapwater.put("z1", "");
mapwater.put("stnm1", "");
mapwater.put("stcd2",listwater2.get(j).get("stcd2"));
mapwater.put("tm2", listwater2.get(j).get("tm2"));
mapwater.put("z2", listwater2.get(j).get("z2"));
mapwater.put("stnm2",listwater2.get(j).get("stnm2"));
listwater.add(mapwater);
}
}
}
JSONObject jsonwater = new JSONObject();
jsonwater.put("data", listwater);
JSONArray jsonArraywaters = jsonwater.getJSONArray("data");//得到list的形式
queryParams.put("water", jsonArraywaters);
还有一种重要的循环请参考:http://blog.csdn.net/qq_23145857/article/details/51658410