如何在java中过滤对象的列表变量?
我有一个对象如何在java中过滤对象的列表变量?,java,java-stream,Java,Java Stream,我有一个对象Student public class Student { String name; String[] classes; public Student(String name, String[] classes){ this.name = name; this.classes = classes; } //getter & setter } 我想过滤这些类。例如,我有 Student sa = new
Student
public class Student {
String name;
String[] classes;
public Student(String name, String[] classes){
this.name = name;
this.classes = classes;
}
//getter & setter
}
我想过滤这些类。例如,我有
Student sa = new Student("John", "math, physics")
Student sb = new Student("Jack", "math, english")
Student sc = new Student("Maria", "math, chemistry")
那么,我如何让
学生
使用数学
VARABLE?我需要写一个新的谓词还是有一个现有的方法?谢谢。您可以使用seapatefind
方法在数组中查找类:
List<Student> mathStudents = studentList.stream()
.filter(student -> findClass(student.getClasses(), "math"))
.collect(Collectors.toList());
你的代码有很多错误 第一:如果构造函数需要
String[]
参数,则必须提供String[]
而不是String
。可以这样称呼:
Student sa = new Student("John", new String[]{"math", "physics"});
Student sb = new Student("Jack", new String[]{"math", "english"});
Student sc = new Student("Maria", new String[]{"math", "chemistry"});
Arrays.stream(new Student[]{sa, sb, sc})
Arrays.stream(new Student[]{sa, sb, sc})
.filter(s -> {
for(String className : s.classes) {
if(className.equals("math")) return true;
}
return false;
})
您应该将字段设置为私有并使用getter,而不是将它们设置为包私有。您可以阅读有关可见性的内容
现在,您有三个Student
对象。你不能像这样用最简单的方法将它们放入流中:
Student sa = new Student("John", new String[]{"math", "physics"});
Student sb = new Student("Jack", new String[]{"math", "english"});
Student sc = new Student("Maria", new String[]{"math", "chemistry"});
Arrays.stream(new Student[]{sa, sb, sc})
Arrays.stream(new Student[]{sa, sb, sc})
.filter(s -> {
for(String className : s.classes) {
if(className.equals("math")) return true;
}
return false;
})
您现在可以使用以下内容过滤流:
Student sa = new Student("John", new String[]{"math", "physics"});
Student sb = new Student("Jack", new String[]{"math", "english"});
Student sc = new Student("Maria", new String[]{"math", "chemistry"});
Arrays.stream(new Student[]{sa, sb, sc})
Arrays.stream(new Student[]{sa, sb, sc})
.filter(s -> {
for(String className : s.classes) {
if(className.equals("math")) return true;
}
return false;
})
这将为您提供一个在其
类
字段中有数学
类的所有学生流。现在,您可以收集元素或对其执行更多操作。假设您有一个填充了正确数据的列表,您可以像这样完成手头的任务:
List<Student> result =
students.stream()
.filter(student -> Arrays.stream(student.getClasses())
.anyMatch("math"::equals))
.collect(Collectors.toList());
列表结果=
学生们。stream()
.filter(student->Arrays.stream(student.getClasses())
.anyMatch(“数学”::等于))
.collect(Collectors.toList());
这将从学生列表中创建一个流,并对其进行筛选,以仅保留拥有“数学”类的学生。从该数组中构建一个流,并按该“术语”进行筛选。编写getter并使用streams lambda和filter Method。您不能仅从“数学,物理”创建一个数组,您需要执行类似于新字符串[]{“数学”,“物理”}我建议在Student
constructor中对数组进行排序。这将更容易在以后查看化学)谢谢你的回答。我可以问一下如何筛选两个类,例如数学
和物理
?谢谢@毫无疑问,您可以这样做-->List result=students.stream().filter(s->{Set classes=Set.of(s.getClasses());return classes.contains(“数学”)和&classes.contains(“物理”);}).collect(Collectors.toList())
@DexD.Hunter如果您没有使用java-9,那么更改Set classes=Set.of(s.getClasses())
toList classes=Arrays.asList(s.getClasses())代码>就像一个符咒。非常感谢你!