巴扎黑2017-04-18 10:35:24
I don’t know what your POJO object is called, I named it Project
@Getter
@Setter
@NoArgsConstructor
public class Project {
private Integer uud;
private String regCode;
private String projectName;
private String projectAddress;
private String companyName;
// 设置分组的key,这里就是把你想要分组的key拼起来
public String groupKey(){
return this.projectName + "_" + this.projectAddress + "_" + this.companyName;
}
}
Then use the Collectors.groupingBy method to group, as follows:
List<Project> projects = new ArrayList<>();
// 这里的key就是,宁江大院_成都市都江堰市蒲阳镇花溪村_成都文森电梯设备股份有限公司
Map<String, List<Project>> group = projects.stream().collect(Collectors.groupingBy(Project::groupKey));
We get a map here, the key is the grouping basis we just put together, and the value is a list, which is the collection under the grouping
Your display is on the page... The map has been divided into groups. You can just cycle through the map according to the rules of your page... I can only process it based on the POJO objects I created.
If there is no way to use Java8, then make a similar map classification yourself, similar to the code below
List<Project> projects = new ArrayList<>();
Map<String, List<Project>> map = new HashMap<>();
for (Project project: projects){
String key = project.groupKey();
// 按照key取出子集合
List<Project> subProjects = map.get(key);
// 若子集合不存在,则重新创建一个新集合,并把当前Project加入,然后put到map中
if (subProjects == null){
subProjects = new ArrayList<>();
subProjects.add(project);
map.put(key, subProjects);
}else {
// 若子集合存在,则直接把当前Project加入即可
subProjects.add(project);
}
}
PHPz2017-04-18 10:35:24
I don’t know if these rows are not repeated after your grouping. For example, SELECT projectName FROM table name GROUP BY projectName; and then it is found to be a resultSet. You instantiate an ArrayList and then loop through it and add it to the list. I don’t know what I’m talking about. Is it what you thought?