Java List 对象条件筛选及计数

在 Java 编程中,List 是一种常用的数据结构,用于存储一组有序的元素。很多时候,我们需要从 List 中筛选符合特定条件的元素,并计算这些元素的数量。本文将深入探讨如何实现这一功能,并通过示例代码帮助理解。

List 和条件筛选

Java 中的 List 接口是一个有序集合,允许重复的元素。我们可以使用 ArrayListLinkedList 等实现类来创建 List 对象。假设我们有一个代表学生的 Student 类,以及一个存储学生对象的 List。我们希望计算出所有成绩大于某个值的学生数量。

Student 类定义

首先,我们定义一个 Student 类,包含姓名和成绩两个属性。

public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
示例代码

接下来,我们可以创建一个 List,存储多个学生对象,并实现一个方法来查找符合条件的学生数量。以下是实现的示例代码:

import java.util.ArrayList;
import java.util.List;

public class StudentFilter {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 70));
        students.add(new Student("Charlie", 90));
        students.add(new Student("David", 65));
        
        int threshold = 75;
        int count = countStudentsAboveScore(students, threshold);
        
        System.out.println("成绩高于 " + threshold + " 的学生数量: " + count);
    }

    public static int countStudentsAboveScore(List<Student> students, int score) {
        int count = 0;
        for (Student student : students) {
            if (student.getScore() > score) {
                count++;
            }
        }
        return count;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

在上述代码中,我们创建了一个学生列表,并通过 countStudentsAboveScore 方法计算出成绩高于 75 分的学生数量。最终,通过 System.out.println 输出结果。

使用 Stream API 进行条件筛选

在 Java 8 及更高版本中,我们可以使用流(Stream API)来简化条件筛选过程。以下是使用流的示例:

import java.util.List;
import java.util.ArrayList;

public class StudentFilter {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 70));
        students.add(new Student("Charlie", 90));
        students.add(new Student("David", 65));

        int threshold = 75;
        long count = students.stream()
                .filter(student -> student.getScore() > threshold)
                .count();

        System.out.println("成绩高于 " + threshold + " 的学生数量: " + count);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

在这个版本中,我们使用 stream() 方法创建一个流对象,并通过 filter 方法筛选出符合条件的学生。最后,使用 count() 方法直接获取符合条件的数量。

关系图示意

为了更好的理解,我们可以构建一个简单的 ER 图来表示 Student 类与其属性之间的关系:

STUDENT string name int score

在该图中,STUDENT 表示学生类,其包含 namescore 两个属性。

结论

通过上述示例,我们了解了如何在 Java 中使用 List 对象筛选符合条件的元素,并计算其数量。我们用简单的循环和 Stream API 进行了不同的实现,展示了 Java 语言的灵活性。此外,ER 图帮助我们简化了类的结构理解。掌握这些基本技巧,对于我们在进行数据处理和分析时至关重要。在日常开发中,我们还可以将这些方法与其他数据操作结合使用,以实现更复杂的数据逻辑处理。希望这篇文章能帮助你在 Java 编程中更好地使用 List