0

i have a class called Student has two variables:

    String lastName;
    String firstName; 

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

    } 

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

    @Override
    public boolean equals(Object obj) {
        String getLastName = ((Student) obj).getLastName();
        return lastName.equalsIgnoreCase(getLastName);
    }

in the Main class i have created an ArrayList

    private static ArrayList<Student> listOfStudents = new ArrayList<>(); 

and i have created this method to get the students from ArrayList

public void findStudent(String lastName){
       for (int i=0;i<listOfStudents.size();i++){
                if (listOfStudents.get(i).equals(new Student(lastName))){
                    System.out.println(listOfStudents.get(i));
                }
       }
}

I have overridden equals() to check by last name. Now everything is well, but when i add duplicate last name like below:

        listOfStudents.add(new Student("Tamoussat","Abdelillah"));
        listOfStudents.add(new Student("Tamoussat","Fatima"));
        listOfStudents.add(new Student("Soussi","Ahlam"));

I get only the first element, i want the way of how to get more than one element if they have the same last name? Best Regards

4
  • 2
    Could you please post your hashcode() and equals() methods Commented Jan 14, 2020 at 18:17
  • 1
    Are you saying that you are getting the first repeated element only but you want all the repeated elements? Commented Jan 14, 2020 at 19:30
  • @SharadNanda exactly Commented Jan 14, 2020 at 20:32
  • @IgorKhvostenkov done Commented Jan 14, 2020 at 20:58

4 Answers 4

2
public class Student {
    private String lastName;
    private String firstName;

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

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return firstName + " " +  lastName;
    }
}

You can filter your list with the java stream api:

public static void findStudent(List<Student> listOfStudents, String lastName){
    listOfStudents.stream().filter(s -> s.getLastName().equals(lastName)).forEach(student -> {
        // for each student with the lastName
        System.out.println(student);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

The mistake will be in your equals() method, as running the code above works as expected with my own implementation of equals() which just compares lastName of this with the object argument.

But still, try to improve this method

public void
findStudent(String lastName)
{
  for (Student student : listOfStudents)
  {
    if (student.lastName().equals(lastName))
    {
      System.out.println(student);
    }
  }
}

or any other version like @CodingSamples 's

Comments

1

Your equals() and hashCode() should work, despite they are not canonical. I would write it in a more better way:

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Student student = (Student) o;
    return Objects.equals(lastName, student.lastName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(lastName);
  }

But this does not explain why your code returns only one item from the array. You can try my implementation or post more precisely code you execute with data you use and the result of execution.

Comments

0

Thanks for helping, finally i found the problem was in my code, i have a method to add students to array, in that method i used


if(!listOfStudents.contains(new Student(lastName,firstName)){}

So the problem was in other method, when it found duplicate element it doesn't add it to the ArrayList, when i deleted this if statement from that method, code worked good, Best Regard

Comments

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.