list根据实体类某个字段合并并处理其他字段值
背景:前段需要展示3张表的数据,如果是同一个人创建的则合并标题(标题不同,但内容相同)
分析:虽然表里还有其他字段不同,但是前段所需的字段一样,所以可以创建一个实体类,查询时只取所需的字段返回
测试代码如下:
先创建实体类
package com.example.demo.java8;
public class Comment {
private String id;
private String name;
private String content;
public Comment() {
}
public Comment(String id, String name, String content) {
this.id = id;
this.name = name;
this.content = content;
}
@Override
public String toString() {
return "MmComment{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", content='" + content + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
实现合并处理方案写了map和Java8两种
package com.example.demo.java8;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestList {
public static void main(String[] args) {
Comment styleComment1 = new Comment("1","社团","测试1");
Comment styleComment2 = new Comment("2","社团","测试2");
Comment styleComment3 = new Comment("3","社团","测试3");
Comment styleComment4 = new Comment("4","社团","测试4");
Comment mmComment1 = new Comment("1","高手","测试1");
Comment mmComment2 = new Comment("12","高手","测试高手");
Comment mmComment3 = new Comment("3","高手","测试3");
Comment mmComment4 = new Comment("24","高手","测试24");
Comment ssComment1 = new Comment("1","星辰","测试1");
Comment ssComment2 = new Comment("2","星辰","测试2");
Comment ssComment3 = new Comment("23","星辰","测试星辰1");
Comment ssComment4 = new Comment("24","星辰","测试24");
List<Comment> list = new ArrayList();
List<Comment> listStyle = new ArrayList();
List<Comment> listMM = new ArrayList();
List<Comment> listSS = new ArrayList();
listStyle.add(styleComment1);
listStyle.add(styleComment2);
listStyle.add(styleComment3);
listStyle.add(styleComment4);
listMM.add(mmComment1);
listMM.add(mmComment2);
listMM.add(mmComment3);
listMM.add(mmComment4);
listSS.add(ssComment1);
listSS.add(ssComment2);
listSS.add(ssComment3);
listSS.add(ssComment4);
list.addAll(listStyle);
list.addAll(listMM);
list.addAll(listSS);
List<Comment> newList2 = new ArrayList<>();
list.parallelStream().collect(Collectors.groupingBy(Comment::getId, Collectors.toList()))
.forEach((id, comments) -> {
comments.stream().reduce((a, b) -> new Comment (a.getId(), a.getName() +","+ b.getName(),
a.getContent())).ifPresent(newList2 ::add);
});
System.out.println("Java8新特性处理开始~~~~~");
for (Comment comment:newList2
) {
System.out.println(comment);
}
System.out.println("Java8新特性处理结束~~~~~");
System.out.println("map处理开始~~~~~");
//Map<String,Comment> map = (Map)listStyle.stream().collect(Collectors.toMap(Comment::getId, comment -> comment));
Map<String,Comment> map1 = (Map)listMM.stream().collect(Collectors.toMap(Comment::getId, comment -> comment));
Map<String,Comment> map2 = (Map)listSS.stream().collect(Collectors.toMap(Comment::getId, comment -> comment));
//去掉重复的key
for(Comment dataReport:listStyle){
String temp = dataReport.getId();
if(map1.containsKey(temp)){
Comment newDataReport= new Comment();
newDataReport.setId(temp);
//合并相同key的value
newDataReport.setName(map1.get(temp).getName()+","+dataReport.getName()+",");
newDataReport.setContent(dataReport.getContent());
//HashMap不允许key重复,当有key重复时,前面key对应的value值会被覆盖
map1.put(temp,newDataReport);
}
else{
map1.put(temp,dataReport);
}
}
//去除重复key的list
List<Comment> newList = new ArrayList<Comment>();
for(String temp:map1.keySet()){
newList.add(map1.get(temp));
}
for(Comment dataReport:newList){
String temp = dataReport.getId();
if(map2.containsKey(temp)){
Comment newDataReport= new Comment();
newDataReport.setId(temp);
//合并相同key的value
newDataReport.setName(map2.get(temp).getName()+","+dataReport.getName());
newDataReport.setContent(dataReport.getContent());
//HashMap不允许key重复,当有key重复时,前面key对应的value值会被覆盖
map2.put(temp,newDataReport);
}
else{
map2.put(temp,dataReport);
}
}
List<Comment> newList1 = new ArrayList<Comment>();
for(String temp:map2.keySet()){
Comment comment = map2.get(temp);
String name = comment.getName();
if (name.endsWith(",")){
name = name.substring(0,name.length()-1);
}
comment.setName(name);
newList1.add(comment);
}
System.out.println(newList1.size());
for (Comment comment:newList1
) {
System.out.println(comment);
}
System.out.println("map处理结束~~~~~");
}
}
相对而言,Java8新特性处理起来更简单,但是对于不了解Java8的同志阅读起来可能稍微难一点。
以上就是处理方案,请大家指正!!!