【问题标题】:group data in arraylist according to same Id根据相同的Id对arraylist中的数据进行分组
【发布时间】:2019-07-28 18:51:37
【问题描述】:

我有一个问题,我想创建一个新的数组列表,因为必须将它发送到其他类。
我无法发布我原来的问题,所以复制了数据。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Grouping {
     List<Student> studlist1 = new ArrayList<Student>();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        List<Student> studlist = new ArrayList<Student>();
        studlist.add(new Student("1", "John", "New York"));
        studlist.add(new Student("1", "Max", "California"));
        studlist.add(new Student("2", "Andrew", "Los Angeles"));
        studlist.add(new Student("3", "Michael", "New York"));
        studlist.add(new Student("3", "Sam", "California"));
        studlist.add(new Student("4", "Mark", "New York"));

        Map<String, List<Student>> groupedStudents = new HashMap<String, List<Student>>();
        for (Student student: studlist) {
            String key = student.stud_id;
            if (groupedStudents.containsKey(key)) {
                List<Student> list = groupedStudents.get(key);
                list.add(student);
                }
            else {
            List<Student> list = new ArrayList<Student>();
            list.add(student);
            groupedStudents.put(key, list);}  
        }


        Set<String> groupedStudentsKeySet = groupedStudents.keySet();
        int index = 0;
        for (String id: groupedStudentsKeySet) {
            List<Student> stdnts = groupedStudents.get(id);

            for (Student student : stdnts) {
//               Want to create a new arraylist as belows.Grouing data in arraylist according to same ID
//                 arraylist will be as
//
//                 index 0: 1,John,New York,Max,Califoornia
//                 index 1: 2, Andrew, Los Angeles
//                 index 2: 3,Michael, New York, "Sam", California
//                 index 3: 4,Mark, New York


            }

        }

    }
}

class Student {

    String stud_id;
    String stud_name;
    String stud_location;

    Student(String sid, String sname, String slocation) {

        this.stud_id = sid;
        this.stud_name = sname;
        this.stud_location = slocation;

    }
}

想创建一个新的arraylist,如下所示。根据相同的ID对arraylist中的数据进行分组

Arraylist 应该是 索引 0:1,约翰,纽约,马克斯,加利福尼亚

索引 1:2,安德鲁,洛杉矶

索引 2:3,迈克尔,纽约,“山姆”,加利福尼亚

索引 3:4,马克,纽约

只想要和上面一样的数组列表

【问题讨论】:

  • 问题的任何解决方案

标签: java arraylist collections


【解决方案1】:
if (groupedStudents.containsKey(key)) {
            groupedStudents.get(key).add(student);
            }

使用此代码。 希望这会对你有所帮助。

【讨论】:

  • 想创建一个新的arraylist。不添加到地图本身
  • 它将学生对象添加到存储在地图中的现有列表中。试试看。
【解决方案2】:

我之前建议检查 Stream Api(https://stackoverflow.com/questions/57236902/group-data-in-arraylist?noredirect=1#comment100976235_57236902) 中的 'groupingBy' 方法,您尝试做的事情可以在一行中完成:

Map<String, List<Student>> collect = studlist.stream().collect(Collectors.groupingBy(Student::getStud_id));

它不是按索引列出的,但我想它更方便。

不要忘记将 getter 添加到您的 Student 类中。

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多