Silly Bean
Silly Bean

Reputation: 31

How do I know if an array list of objects contain a certain string?

I made a method that search through an array list of objects. Then if the searchKey is found in the array list it will print this certain item.

Here is how I iterate through the array list if it contains the searchKey, but I just realized that it is impossible to compare a string and an object.

for(int x = 0; x < Student.students.size(); x ++){
    if(Student.students.contains(searchKey))
        System.out.print(Student.students.get(x));
}

Here's how I create the constructor and array list.

String firstName, lastName, course, yearLevel, gender;
    
    Student(String firstName, String lastName, String course, String yearLevel, String gender)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.course = course;
        this.yearLevel = yearLevel;
        this.gender = gender;
    }

    static ArrayList<Student> students = new ArrayList<Student>();

Upvotes: 2

Views: 110

Answers (2)

vsfDawg
vsfDawg

Reputation: 1527

You haven't defined what 'contains' means here but I'm going to assume that it means a Student contains the key if it appears as a substring in any of its String members. So let's start with a method that does that. We can define this as part of the Student class itself.

public class Student {
  .... other stuff ....
  /**
   * Return true if any of the properties of this Student
   * contain the given substring, false otherwise
   */
  public boolean contains(String s) {
    // consider addressing null cases - omitting for simplicity
    return firstName.contains(s) ||
      lastName.contains(s) ||
      course.contains(s) ||
      yearLevel.contains(s) ||
      gender.contains(s);
  }
}

Now you can iterate over your List and invoke this method to find the matches. Note that you need to handle the case that multiple Students may match a given search key (or none may match). So I would suggest collecting the results in a separate List. One does not generally iterate over Lists via the index. This example uses an enhanced for-loop (aka for-each).

public List<Student> findMatches(List<Student> students, String key) {
  List<Student> found = new ArrayList<>();
  for (Student s : students) {
    if (s.contains(key)) {
      found.add(s);
    }
  }
  return found;
}

This is a good case for using the Stream API.

public List<Student> findMatches(List<Student> students, String key) {
  return students.stream()
      .filter(s -> s.contains(key))
      .collect(Collectors.toList());
}

Upvotes: 1

azro
azro

Reputation: 54168

You need to compare the one property; also you can use a for-each loop to simplify the code

for(Student s : Student.students){
    if(s.getName().equals(searchKey))
        System.out.print(s);
}

Note :

When you use a condition Student.students.contains(searchKey) in a loop, and it doesn't use the iteration variable that means there is a problem

Upvotes: 2

Related Questions