Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList's custom Contains method

I have some object

class A {   private Long id;    private String name;    public boolean equals(Long v) {      return this.id.equals(v);   } } 

and ArrayList of these objects. What I want is to be able to check if that list contains some object by object's field. For example:

ArrayList<A> list = new ArrayList<A>(); if (list.contains(0L)) {...} 

but overrided Equals method is not helps me. What I am doing wrong? Thank you

UPDATE And should I override a hashcode() method too?

like image 654
nKognito Avatar asked Nov 30 '11 06:11

nKognito


People also ask

Does ArrayList contain contains method?

Java ArrayList contains()The contains() method checks if the specified element is present in the arraylist.

How does contains method work in Java ArrayList?

ArrayList contains() MethodThis method takes one object as its parameter. It checks if this object is in the ArrayList or not.It returns one boolean value. If the ArrayList contains at least one element, then it returns true. Else it returns false.

How do I check if an ArrayList contains an item?

ArrayList. contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.

How do you check if an ArrayList of objects contains a value in Java?

To check if ArrayList contains a specific object or element, use ArrayList. contains() method. You can call contains() method on the ArrayList, with the element passed as argument to the method. contains() method returns true if the object is present in the list, else the method returns false.


1 Answers

here's some code that might demonstrate how it works out:

import java.util.ArrayList;  class A {   private Long id;    private String name;     A(Long id){       this.id = id;   }      @Override   public boolean equals(Object v) {         boolean retVal = false;          if (v instanceof A){             A ptr = (A) v;             retVal = ptr.id.longValue() == this.id;         }       return retVal;   }      @Override     public int hashCode() {         int hash = 7;         hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);         return hash;     } }  public class ArrayList_recap {     public static void main(String[] args) {         ArrayList<A> list = new ArrayList<A>();           list.add(new A(0L));         list.add(new A(1L));          if (list.contains(new A(0L)))         {             System.out.println("Equal");         }         else         {             System.out.println("Nah.");         }         }  } 

First, there is an override of the equals(Object o) method. Then there is the override of the hashCode() as well. Also note that the instanceof A check in the equals will ensure that you're not trying to compare different objects.

That should do the trick! Hope it helped! Cheers :)

like image 196
Vern Avatar answered Sep 21 '22 15:09

Vern