1

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) ?)

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

I used HashMap instead and it's even more efficient. Thank you
2

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();
    }
}

Comments

1

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;

Comments

1

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;
        }

Comments

0

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.

1 Comment

Darn it. Charly beat me to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.