-1

I have an array list full of objects, i want to check if there are any objects with the name property equal to "maths".

How can this be done?

if(courses.contains(students.get(i).chosenPathway)){

Right now im using this but it doesnt work as courses is an array list of objects

guynumerouno
  • 157
  • 3
  • 14

3 Answers3

2

You should add more information next time about the name of the variables but i think i can get what u mean. You need to check each object attribute from the list and compare it with equals for example like this:


for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).chosenPathway().equalsIgnoreCase("maths")) {
System.out.println("An object contains maths as chosen pathway.");
break;
}

}


MadMax
  • 91
  • 3
  • 14
1

Loop through the array and then follow your logic. I assume that Course contains a list named students, from which you need to know if their chosenPathWay is "maths".

yourArray is the array that holds all Courses.

List<Student> matchingStudents=new ArrayList<Student>();

for (int i=0;i<yourArray.length();i++)
{
   Course c = yourArray[i];
   for (Student student: c.students)
      if (student.chosenPathway.equalsIgnoreCase(requiredPath)) //requiredPath = "maths"
          matchingStudents.add(student);

}

The code above assumes you must store all students that match the criteria. If you only need to know wether a Course has an student whose path is maths, just:

List<Course> matchingCourses =new ArrayList<>();

for (int i=0;i<yourArray.length();i++)
{
   Course c = yourArray[i];
   for (Student student: c.students)
      if (student.chosenPathway.equalsIgnoreCase(requiredPath)) //requiredPath="maths"
      {   
         matchingCourses.add(c);
         break;                  //finish looping through this course
       }
 }
aran
  • 10,978
  • 5
  • 39
  • 69
  • aran - Good answer. A couple of points: (1) In the second solution, you do not need a `List`, a `String` variable will be sufficient to store the first matching value. (2) You can also add a solution using the `Stream` API. – Arvind Kumar Avinash Dec 17 '20 at 20:58
  • You are right about the second list being unnecesary. But about your 2nd point, i just hate Streams ; ) It's a personal thing – aran Dec 17 '20 at 21:00
  • aran - Ha ha ...`Stream` is not so bad :) – Arvind Kumar Avinash Dec 17 '20 at 21:02
  • 1
    Really, I do hate it. Working it with my therapist. Ask @Stephen C, he knows --> https://stackoverflow.com/a/65164081/2148953 – aran Dec 17 '20 at 21:04
-1

Let's say you have a list as below

List<Student> studentsList = new ArrayList<>();

Prior to Java-8

for(Student stud : StudentList){
   if("maths".equals(stud.getChosenPathway())){
    //your logic goes here
    }
}

With Java-8 and above

to fetch single object

Optional<Student> stud = studentList.stream().findFirst().filter(s -> s.getChosenPathway().equals("maths"));

get all the objects

List<Student> empl = studentsList.stream().filter(s -> s.getName().equals("maths")).collect(Collectors.toList());
Kashyap
  • 385
  • 3
  • 13
  • the correct way would be this: Optional stud = studentList.stream().filter(s -> s.getChosenPathway().equals("maths")).findFirst(); why did you put findFirst() in the first place ? that way you filter only find one element – Lucke Oct 13 '21 at 10:23