Saturday, February 22, 2020

Find the custom class object in ArrayList

Searching in a ArrayList with custom objects for certain object?
Find the custom class object in ArrayList ? Or Searching for a specific object in an ArrayList?

In this example you we learn that how the contains method work with Arraylist while using custom class object.
First we check without override equals() method.

package arraylist;

public class Student {

 private int id;
 private String name;

 public Student(int id, String name) {
  this.name = name;
  this.id = id;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "{Id:" + id + ",Name:" + name + "}";
 }
}
package arraylist;

import java.util.ArrayList;

public class MainArrayListTest {

 // find the custom class object in ArrayList
 public static void main(String[] args) {

  Student s1 = new Student(1, "Alex");
  Student s2 = new Student(2, "Amit");
  Student s3 = new Student(3, "Sumit");

  ArrayList<Student> ls = new ArrayList<Student>();
  ls.add(s1);
  ls.add(s2);
  ls.add(s3);

  Student find = new Student(3, "Sumit");

  if (ls.contains(find)) {
   System.out.println(find);
  } else {
   System.out.println("Object not found in list.");
  }
 }
}
Output:
Object not found in list.

In the above example. As you can see. I have add student id:3 and name:Sumit but while i search the student with same details using contains method its return false thatwhy its fall into else block.
So to full fill the requirment we need to override the equals() methods in student class.

Now we check with override equals() method.
package arraylist;

public class Student {

 private int id;
 private String name;

 public Student(int id, String name) {
  this.name = name;
  this.id = id;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public boolean equals(Object obj) {
  if (obj == null)
   return false;
  if (!(obj instanceof Student))
   return false;
  if (obj == this)
   return true;
  return this.getId() == ((Student) obj).getId();
 }

 @Override
 public String toString() {
  return "{Id:" + id + ",Name:" + name + "}";
 }
}
package arraylist;

import java.util.ArrayList;

public class MainArrayListTest {

 // find the custom class object in ArrayList
 public static void main(String[] args) {

  Student s1 = new Student(1, "Alex");
  Student s2 = new Student(2, "Amit");
  Student s3 = new Student(3, "Sumit");

  ArrayList<Student> ls = new ArrayList<Student>();
  ls.add(s1);
  ls.add(s2);
  ls.add(s3);

  Student find = new Student(3, "Sumit");

  if (ls.contains(find)) {
   System.out.println(find);
  } else {
   System.out.println("Object not found in list.");
  }
 }
}
Output:
{Id:3,Name:Sumit}
Now this contains method return true and the output as expected. So its clear from the above example that we must override equals() method. whils we need to search custom object in ArrayList.

No comments:

Post a Comment