合并和拆分map,合并和拆分list,应用于把表格的两行并排显示

合并两个map:

eg1.

public static void main(String args[])
  {
   Map  map =new HashMap();
   Map  map1 =new HashMap();
   
   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那么用后面的覆盖前面的



像下面水位这样的表,循环一排显示两个数据的:原理还是一排显示一个数据,”只不过这个数据里有两个数据

合并和拆分map,合并和拆分list,应用于把表格的两行并排显示_第1张图片

合并和拆分map,合并和拆分list,应用于把表格的两行并排显示_第2张图片

  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> listwater =new ArrayList();

List> listwater1 =new ArrayList();
List> listwater2 =new ArrayList();
List> listwaters =new ArrayList();
//拆分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的形式


String urlrain="http://192.168.1.15:8080/pingshan-service/data/Query1?sttp=PP";
String datarain=result(urlrain);
int indexdaorain=datarain.indexOf("{");
String resultdaorain=datarain.substring(indexdaorain);
String resultdaorain2=resultdaorain.replace(")","");System.out.println("resultdaorain2"+resultdaorain2);
// 得到一个json对象
JSONObject jsObjrain = JSONObject.fromObject(resultdaorain2);
JSONArray jsonArrayrain = jsObjrain.getJSONArray("data");

String urlneilao="http://192.168.1.15:8080/pingshan-service/data/Query1?sttp=NL";
String dataneilao=result(urlneilao);
int indexdaoneilao=dataneilao.indexOf("{");
String resultdaoneilao=dataneilao.substring(indexdaoneilao);
String resultdaoneilao2=resultdaoneilao.replace(")","");
// 得到一个json对象
JSONObject jsObjneilao = JSONObject.fromObject(resultdaoneilao2);
JSONArray jsonArrayneilao = jsObjneilao.getJSONArray("data");


HashMap queryParams = new HashMap();

           queryParams.put("water", jsonArraywaters);
           queryParams.put("rain", jsonArrayrain);
           queryParams.put("neilao", jsonArrayneilao);
           
return new ModelAndView("thereals", queryParams);


}

//根据接口地址获取数据
public String result(String urls) {
String url = urls;
// 地址对象
URL webUrl = null;
try {
webUrl = new URL(url);
} catch (MalformedURLException e) {

e.printStackTrace();
}
// 建立远程连接
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) webUrl.openConnection();
} catch (IOException e) {

e.printStackTrace();
}
// 得到输入流
InputStream ins = null;
try {
ins = conn.getInputStream();
} catch (IOException e) {

e.printStackTrace();
}


// 字符读取流
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new InputStreamReader(ins,
"UTF-8"));
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
}
// 使用字符串构造器,将读取的每一行数据拼接起来
StringBuffer sb = new StringBuffer();


String content = null;
try {
while ((content = bfr.readLine()) != null) {
sb.append(content);
}
} catch (IOException e) {

e.printStackTrace();
}
// 关闭字符流
try {
bfr.close();
} catch (IOException e) {

e.printStackTrace();
}
// 释放连接
conn.disconnect();
// 得到返回的字符串
String data = sb.toString();
return data;
}

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> listwater =new ArrayList();

List> listwater1 =new ArrayList();
List> listwater2 =new ArrayList();
List> listwaters =new ArrayList();
//拆分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

你可能感兴趣的:(合并和拆分map,合并和拆分list,应用于把表格的两行并排显示)