![](/img/trans.png)
[英]Suppose I have 5 objects. I want the data of this particular object be accessible by 2 objects but not the other two. How can I do that?
[英]I want to check if a column has specific values in list of entity objects.?How can i do that?
我有一個Student
對象studentList
的列表。 每個student
都有一個name
和一個marks
屬性。
我想檢查標記列是否包含值>=60
和<70
。 除了循環每個學生的列表並檢查價值之外,還有其他方法可以檢查嗎?
正如@Amongalen 建議的那樣,您可以使用Stream#filter
:
List<Student> result = studentList.stream()
.filter(student -> student.marks>=60 && student.marks <70)
.collect(Collectors.toList());
您可以在https://mkyong.com/java8/java-8-streams-filter-examples/中找到更詳細的解釋
Student
樣本 class:class Student{
String name;
int marks;
public Student() {
super();
}
public Student(String name, int marks) {
super();
this.name = name;
this.marks = marks;
}
public String getName() {
return name;
}
public int getMarks() {
return marks;
}
public void setName(String name) {
this.name = name;
}
public void setMarks(int marks) {
this.marks = marks;
}
@Override
public String toString() {
return "Student [name=" + name + ", marks=" + marks + "]";
}
}
Test
的主要方法:public class Test{
public static void main(String []args){
List<Student> list = new ArrayList<Student>();
list.add(new Student("ABC",65));
list.add(new Student("XYZ",85));
list.add(new Student("CDE",55));
list.add(new Student("FGH",75));
List<Student> resultList = list.stream()
.filter(o -> (o.marks>=65 && o.marks<75))
.collect(Collectors.toList());
System.out.println(resultList );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.