JAVA项目实战,接口集合的数据形式的调整

来源:互联网 发布:微信数据转移到sd卡 编辑:程序博客网 时间:2024/06/11 05:59
package www.myListSort.com;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import www.listchuli.com.TestList;


public class MyListSortTest {
public static void main(String[] args){
Map<String,Object> mapResult11=new HashMap<String,Object>();
mapResult11.put("questiontype", 1);
mapResult11.put("question", "1.如何修改密码");
mapResult11.put("answer", "A:已绑定的有效手机可以进入个人中心修改");

Map<String,Object> mapResult12=new HashMap<String,Object>();
mapResult12.put("questiontype", 1);
mapResult12.put("question", "2.如何注销账号");
mapResult12.put("answer", "A:注销账号");

Map<String,Object> mapResult13=new HashMap<String,Object>();
mapResult13.put("questiontype", 1);
mapResult13.put("question", "3.如何切换账号");
mapResult13.put("answer", "A:切换账号的方法");

   Map<String,Object> mapResult21=new HashMap<String,Object>();
mapResult21.put("questiontype", 2);
mapResult21.put("question", "1.阅点有无有效期");
mapResult21.put("answer", "A:永久不过期");


Map<String,Object> mapResult22=new HashMap<String,Object>();
mapResult22.put("questiontype", 2);
mapResult22.put("question", "2.阅点如何查询");
mapResult22.put("answer", "A:个人中心我的账户中即可查询");

Map<String,Object> mapResult31=new HashMap<String,Object>();
mapResult31.put("questiontype", 3);
mapResult31.put("question", "1.如何获取积分");
mapResult31.put("answer", "获取积分的方法");

Map<String,Object> mapResult41=new HashMap<String,Object>();
mapResult41.put("questiontype", 4);
mapResult41.put("question", "1.关于免费政策");
mapResult41.put("answer", "免费政策解读");

Map<String,Object> mapResult51=new HashMap<String,Object>();
mapResult51.put("questiontype", 5);
mapResult51.put("question", "1.怎么发表文章");
mapResult51.put("answer", "发表文章的犯法");

   List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
   list.add(mapResult11);
   list.add(mapResult12);
   list.add(mapResult13);
   list.add(mapResult21);
   list.add(mapResult22);
   list.add(mapResult31);
   list.add(mapResult41);
   list.add(mapResult51);
/* 原数据   [{question=1.如何修改密码, answer=A:已绑定的有效手机可以进入个人中心修改, questiontype=1},
    {question=2.如何注销账号, answer=A:注销账号, questiontype=1}, 
    {question=3.如何切换账号, answer=A:切换账号的方法, questiontype=1}, 
    {question=1.阅点有无有效期, answer=A:永久不过期, questiontype=2}, 
    {question=2.阅点如何查询, answer=A:个人中心我的账户中即可查询, questiontype=2}, 
    {question=1.如何获取积分, answer=获取积分的方法, questiontype=3}, 
    {question=1.关于免费政策, answer=免费政策解读, questiontype=4}, 
    {question=1.怎么发表文章, answer=发表文章的犯法, questiontype=5}]


 处理后的数据  result=[{name=关于账号 , questiontype=1, value=[{question=1.如何修改密码, answer=A:已绑定的有效手机可以进入个人中心修改}, {question=2.如何注销账号, answer=A:注销账号}, {question=3.如何切换账号, answer=A:切换账号的方法}]}, 
   {name=关于资费  , questiontype=2, value=[{question=1.阅点有无有效期, answer=A:永久不过期}, {question=2.阅点如何查询, answer=A:个人中心我的账户中即可查询}]}, 
   {name=关于积分 , questiontype=3, value=[{question=1.如何获取积分, answer=获取积分的方法}]}, 
   {name=关于免费政策 , questiontype=4, value=[{question=1.关于免费政策, answer=免费政策解读}]}, 
   {name=其他问题, questiontype=5, value=[{question=1.怎么发表文章, answer=发表文章的犯法}]}]*/
 
   //对list数据进行处理
String idColumn="questiontype";
String[] parentColumn={"questiontype"};
String[] childColumn={"question","answer"};
String collectionColumn="value";
Map<Object,Map<String,Object>> map=new LinkedHashMap<Object,Map<String,Object>>();
   for(int i = 0;i<list.size();i++){
    Map<String,Object> row=list.get(i);
    Object id=row.get(idColumn);
    Map<String,Object> parentRow=map.get(id);
    if(parentRow==null){
    parentRow=new HashMap<String,Object>();
    for(String Column:parentColumn){
    parentRow.put(Column,row.get(Column));
    }
    parentRow.put(collectionColumn, new ArrayList<Map<String,Object>>());
    map.put(id, parentRow);
    }
    @SuppressWarnings("unchecked")
List<Map<String,Object>> childTable=(List<Map<String, Object>>) parentRow.get(collectionColumn);
    Map<String,Object> chlidRow=new HashMap<String,Object>();
    for(String column:childColumn){
    chlidRow.put(column, row.get(column));
    }
    childTable.add(chlidRow);
   }
   list= new ArrayList(map.values());
   for(Map<String, Object> row:list){
    int questiontype=(int) row.get(idColumn);
   
    switch(questiontype){
    case 1:row.put("name", "关于账号");break;
    case 2:row.put("name", "关于资费");break;
    case 3:row.put("name", "关于积分");break;
    case 4:row.put("name", "关于免费政策");break;
    default:row.put("name", "其他问题");
    }
   }
   System.out.println("list"+list);
   
   
}


}