java - 将一个list按照下面字段分组后放入到一个新的list里面
天蓬老师
天蓬老师 2017-04-18 10:33:24
0
3
622

像按以下格式展现内容

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
巴扎黑

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);
            }
        }
PHPzhong

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?

洪涛

Collection.sort(list,new Compararor<>(){

})

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template