Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two objects are Equal in Array List

I have a main class that creates an arraylist of type Element:

public static void main(String[] args) throws IOException {
String input = "";
String id = ""; //combination of letters and numbers
String name = "";

ArrayList<Element> element = new ArrayList<> ();

BufferedReader in = new BufferedReader( new InputStreamReader(System.in));

while(!(input.equalsIgnoreCase("quit"))) {
    System.out.println("Please enter 'e' to enter an element, or 'quit' to quit");
    input = in.readLine();

if(input.equalsIgnoreCase("e")) {

    System.out.println("Please enter a name for the element");
    name = in.readLine();

    System.out.println("Please enter an id for the element");
    id = in.readLine(); 

    element.add(new Element(name,id));
    //only add if id and name don't exist already
}

}

}

Then I have a element clas:

public class Element {

private String name;
private String id;

public Element(String name, String id) {

this.name = name;
this.id = id;

}


}

I want to check before adding an element to a list (it's id and name), to check if another element already in the list already has those exact values (id and name). I know I can use the toString method to do this, but I'm not sure how I can override it to pass on an id and name, before adding the elements to the list. Is their a way to do this? Ideally I'd only want to add an element, if it doesn't already exist.

like image 641
user3739406 Avatar asked Apr 03 '26 03:04

user3739406


2 Answers

Below overridden equals method will work.

public class Element {
    private String name;
    private String id;

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

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Element){
            Element element = (Element) obj;
            if(element != null && this.name.equals(element.name) && this.id.equals(element.id)){
                return true;
            }
        }
        return false;
    }
  }

And then try doing check before adding to your list, as below:

        ArrayList<Element> element = new ArrayList<Element> ();
        Element element1 = new Element("a", "1");
        Element element2 = new Element("b", "2");
        Element element3 = new Element("b", "2");
        element.add(element1);
        element.add(element2);
        if(element.contains(element3)){
            System.out.println("Yes");
        } else{
            System.out.println("No");
        }


Time for some concept:

Below is the contains implementation from Java:

Part which should concern you is this - o.equals(elementData[i]. Internally equals method of the object will be used to do equals check, if you have not overridden the equals method then the memory location of 2 objects will be compared, and since it would be different ArrayList.contains will return false, so by overriding the equals method we are establishing a logical condition on when to treat 2 Element object as same.

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

  public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}
like image 66
hagrawal Avatar answered Apr 08 '26 06:04

hagrawal


You should use a Set. It is a data structure that does exactly what you want it to - it can't contain duplicates. In order for the set to work, you should have a correct implementation of the equals() and hashCode() methods of your class.

like image 42
Danail Alexiev Avatar answered Apr 08 '26 06:04

Danail Alexiev