vedran
vedran

Reputation: 2188

Java, how do I search a specific variable of Objects saved in a array list

I have a Personnel class, where I store all of Objects of type student, professor, tutor...

class Personnel {
     ArrayList<Student> activeStudentsList = new ArrayList<Student>();
}

and I have a class Student

class Student extends Person {
    public Student (String studentID, String firstName, String lastName) {
        super(firstName, lastName);
        this.studentID = studentID;
    }
}

Now, what I want to do before adding a student into array list, is to check if he's already there. I was using this:

private boolean isStudentActive(String studentID) {
    if (activeStudentsList.contains(studentID)) {
        System.out.println("The student " + studentID + " is already on the list.");
        return true;
    } else
        return false; 
}

The problem is, my array list is ArrayList<Student>, so I can't search for a specific String (studentID). How do I do this? How do I only search String studentID of each object on the list? EDIT: (Is there somethin like activeStudentsList.studentID.contains(studentID) ?)

Upvotes: 1

Views: 24421

Answers (6)

Steve J
Steve J

Reputation: 2674

OK, here's my version. Avoid loops and terminating conditions and whatnot, and just program functionally using Google's Guava methods:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.TreeMultimap;

public class TestClass {

    class Person {
        private String firtName = null;
        private String lastName = null;

        public Person(String firtName, String lastName) {
            super();
            this.firtName = firtName;
            this.lastName = lastName;
        }
    };

    class Student extends Person {
        private String studentID = null;

        public Student(String studentID, String firstName, String lastName) {
            super(firstName, lastName);
            this.studentID = studentID;
        }

        public String getStudentID() {
            return studentID;
        }
    }

    class Personnel {
        ArrayList<Student> activeStudentsList = new ArrayList<Student>();

        public boolean isStudentActive(final String studentID) {
            return Iterables.size(Iterables.filter(activeStudentsList,
                    new Predicate<Student>() {
                        @Override
                        public boolean apply(Student arg0) {
                            return arg0.getStudentID().equals(studentID);
                        }
                    })) == 1;
        }
    }

}

It might seem a bit top-heavy for this sort of search, but I find using Filters and Transforms very useful in more complicated scenarios.

Upvotes: 0

charly&#39;s
charly&#39;s

Reputation: 299

Enjoy your time and take the time to use Guava libraries : http://code.google.com/p/guava-libraries/.

try
        {
            Person item = Iterables.find(this.personList,
                    new Predicate<Person>() {
                        public boolean apply(Person q)
                        {
                            return itemId.equals(q.getId());
                        }
                    });
            return item;
        } catch (NoSuchElementException exception)
        {
            return null;
        }

Upvotes: 1

Wizetux
Wizetux

Reputation: 756

Assuming that the studentID string is public or that you have a public Get method for it, you can use the Java For-each loop to check each element in the list:

for (Student s : activeStudentsList)
{
   if (s.studentID == studentID)
      return true;
}
return false;

Upvotes: 1

Java Drinker
Java Drinker

Reputation: 3167

A very simplistic implementation based on Bhesh's mention of making equals and hashcode use the student id:

class Student extends Person {

    private String studentID;

    public Student (String studentID, String firstName, String lastName) {
        super(firstName, lastName);
        this.studentID = studentID;
    }

    @Override
    public boolean equals(Object object) {
        if(object instanceof Student) {
            Student s = (Student) object;
            return this.studentID.equals(s.studentID);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return studentID.hashCode();
    }
}

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Your Student needs to implement the equals and hashCode properly and you can just use List.contains.

Upvotes: 10

JB Nizet
JB Nizet

Reputation: 692231

Iterate through the list, test if the current student's ID is equal to the one you're looking for. If yes, return true, else continue. At the end of the iteration, return false.

Or, if you want something easier and faster, use a Map<String, Student> instead of the list, storing the students indexed by their ID. You may use a HashMap, or LinkedHashMap if you need to preserve the insertion order like a List does.

Upvotes: 4

Related Questions