Java Group的功能

时间:2014-11-13 11:59:55

标签: java list grouping

让我们说我有一个Java课程和学术专业的学生。具有以下变量:SchoolId,BatchId,SectionID,studentID,SubjectID,Marks,Marks2

然后我有一个包含许多值的类的列表。 (列表)

现在我想通过将不同列的组合分组来找到Marks和Marks2的SUM,即 我想把它分组:

  1. SchoolId + BatchId + SectionID
  2. SchoolId + BatchId + SectionID + studentID
  3. SchoolId + BatchId + SectionID + studentID + SubjectID
  4. 这很容易通过SQL实现,但在Java中分组的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

看看这个简单的准备运行示例:

    public static void main(String[] args) {

         class Academics {

            String schoolID, sectionID, studentID;

            public Academics(String schoolID, String sectionID, String studentID) {
                this.schoolID = schoolID;
                this.sectionID = sectionID;
                this.studentID = studentID;
            }

            @Override
            public String toString() {
                StringBuilder builder = new StringBuilder();
                builder.append("Academics [schoolID=");
                builder.append(schoolID);
                builder.append(", sectionID=");
                builder.append(sectionID);
                builder.append(", studentID=");
                builder.append(studentID);
                builder.append("]");
                return builder.toString();
            }

        }
        List<Academics> academics = new ArrayList<>(Arrays.asList(
                new Academics("A", "A1", "1"),
                new Academics("B", "A2", "2"),
                new Academics("A", "A4", "3"),
                new Academics("C", "A1", "4"),
                new Academics("D", "A1", "5"),
                new Academics("C", "A6", "6"),
                new Academics("A", "A2", "7"),
                new Academics("B", "A2", "8")
        ));


        List<Comparator<Academics>> columnComparators = new ArrayList<>();

        // Group By: schoolID + sectionID + studentID
        columnComparators.add(new Comparator<Academics>() {

            @Override
            public int compare(Academics a, Academics b) {

                int bySchoolId = a.schoolID.compareTo(b.schoolID);

                if(bySchoolId == 0){ // equal

                    int bySectionID = a.sectionID.compareTo(b.sectionID);

                    if(bySectionID == 0){// equal
                        return a.studentID.compareTo(b.studentID);
                    }
                    return bySectionID;
                }

                return bySchoolId;
            }
        });

        //... add more Comparators

        Collections.sort(academics, columnComparators.get(0)); // clickedColumn.getIndex()

        for (Academics a : academics) {
            System.out.println(a);
        }

    }

也许你需要这样的东西。

答案 1 :(得分:0)

代码配方

  1. Academics课程定义3个不同的比较器;一个一个 你提到的具体细节。比较器SchoolBatchSection 只考虑他们的schoolId,会考虑两个Academics对象, batchId和sectionId是相等的。同样定义比较器 SchoolBatchSectionStudentSchoolBatchSectionStudentSubject

  2. 除了每个列表的原始列表外,还要定义3个附加列表 你的具体细节,并用每个的不同值填充它们 具体。即list1将仅包含您的不同值 从眼睛看到平等的原始清单 SchoolBatchSectionlist2将包含不同的元素w.r.t. SchoolBatchSectionStudentlist3 w.r.t. SchoolBatchSectionStudentSubject

  3. 定义3个函数(如果可以通过,则定义一个高阶函数 第一类函数作为参数来迭代原始函数 按照要求的标准列出并总结标记。

  4. 遍历三个列表并为每个不同的值调用您定义的合适函数并打印它返回的总和。