因为HashMap是键值对形式,所以一个键只对应一个Value,利用这个原理,我们就可以根据某列重复数据做键对重复数据进行处理啦~
首先先看看我要处理 的数据
然后我最后想要的结果:
需求:根据groupName每个分组信息只显示一条数据就可以了
处理代码如下:
这里我的数据都是从json串中取出来的~
private List<Entity> parseResult(String result) {
List<Entity> list=new ArrayList<Entity>();
HashMap<String,Entity> mymap = new HashMap<String, Entity>();
try {
JSONArray json = new JSONArray(result);
for(int i=0;i<json.length();i++){
JSONObject jsonobject1=json.getJSONObject(i);
Entity mGroup =new Entity();
mGroup.setiD(Integer.valueOf(jsonobject1.getString("iD")));
mGroup.setGroupName(jsonobject1.getString("groupName")); //需要根据晒选数据的列
mGroup.setDepartmentID(Integer.valueOf(jsonobject1.getString("employeeID")));
mymap.put(mGroup.getGroupName(), mGroup); //以分组名为键,实体类为值放入hashmap中
}
//从hashmap中把值遍历出来放入list集合中
for(mGroupWork group:mymap.values()){
list.add(group);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
好了,这样数据处理就算完成了~过程可能麻烦,但也是一种思路~