0

I have to make implement the data structure for a search engine. So there are different type of objects in my DS. I have a generic linked list and I want to only add those elements to the list which are not a part of the list now. Is there a method for comparing two object types in a more exhaustive way because .equals() is returning true every time?

public Boolean IsMember(T o) {
    NodeX<T> temp = head;

    while(temp!= null) {
        if (temp.data.equals(o))
            {//System.out.println("exec");
            return true;}
        temp = temp.next;
    }

    return false;
}

This is the isMember method of the LinkedList. This works only for string types. What can I change in this to make it work for all object types?

If I define an isEqual method for different objects that I am using, how can I use those methods here?

2 Answers2

1

You'll have to implement your version of .equals(). To compare lists here are some questions would be helpful for you

returning difference between two lists in java

Simple way to find if two different lists contain exactly the same elements?

Java Compare Two Lists

Mr. Om
  • 248
  • 2
  • 10
0

Whenever you define an isEqual method for different objects, that is the method that is used whenever isEqual is called, like in isMember.

Kevin McCann
  • 511
  • 5
  • 13