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);
}
}
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);
}
}
阅读全文
0 0
- JAVA项目实战,接口集合的数据形式的调整
- java几种类的形式以及接口的形式
- java获取整形数据的二进制形式
- Hibernate 集合的加载形式
- java数据集合Map接口
- 数据预处理的形式
- FHIR的REST接口形式
- Java Collection 集合类大小调整带来的性能消耗
- Hadoop2.6.0HDFS的java接口实战
- Java接口的游戏实战应用
- java 集合中主要接口的介绍
- Java 集合Collection接口的基本方法
- java 集合框架的接口之一:RandomAccess
- Java集合中Map接口的使用方法
- Java集合中Map接口的使用方法
- Java集合Collection接口的一些方法
- 固定资产的调整分配接口
- 固定资产的调整分配接口
- 入门赛8
- Eclipse内置SVN下载
- 使用Spring的@Scheduled实现定时任务以及解决无法执行定时任务
- opencv如何读取图像、视频(基于Python语言)
- 【数据平台】Eclipse+Scala开发环境(本机和集群两个运行模式)
- JAVA项目实战,接口集合的数据形式的调整
- Java语法--通配符的上界通配符和下界通配符
- Faster-RCNN实现物体检测
- Android动画之animation-list实现逐帧动画
- 如何在html上添加背景音乐
- WEB前端屏幕适配度
- BSON与JSON的区别
- java.util.ConcurrentModificationException异常最简单的解决办法
- 表情键盘 ---> 小思路