1

I have to make an ArrayList that contains an object, the object has one int for year lets say 1 and I don't what another object with the same year 1.

If one object has the int = 1 , i dont want another object with that int(1) in my list. i want to deny it.

Should I try using equal?

something like

@Override
public boolean equals(Object o){
    Object object = (Object)o;
    return this.getInt.equals(object.getInt());
}
0

5 Answers 5

3

Either use a Set...which explicitly disallows duplicates, or check if the list contains the element on insertion.

@Override
public boolean add(T element) {
    if(contains(element)) {
        return false;
    } else {
        return super.add(element);
    }
}

Overriding equals wouldn't get you very far, as you'd be overriding it for the List itself (i.e. you'd be checking if two lists were equal).

Sign up to request clarification or add additional context in comments.

4 Comments

How does that work? i care only about the one int that the Object has, if it has 2 ints, how does this work? i need both ints to be equal to make it count as a duplicated?
If the list contains the element, then you don't want to add it. You're not invoking equals on the list itself; you want equals invoked on the object you're passing in. I'd reference the documentation for List.contains to see how it really works. (You'd also want the equals method to be well-defined for your use case on the entity you pass in - this is why I used a generic type instead of Object.)
Prefer composition over inheritance. You're missing addAll, retainAll, add(int, Object).
@SteveKuo The example was meant to be informative rather than be feature complete.
1

Perhaps you can try using a HashMap linked that links that "int" with the object. That could be:

Map<Integer, Object> map = new HashMap<>();
map.put(object.getInt(), object);
...
//Each time you put a new object you could try this:
if(!map.contains(object.getInt()))
    map.put(object.getInt, object);
//And you can retrieve your object by an int
int a = 1;
Object obj = map.get(1);

Comments

0

In this case, as the value is of type int, you can use equal operator.

public boolean equals(Object o){
    Object object = (Object)o;
    return (this.getInt()==object.getInt());
}

For this kind of requirement, ArrayList is not suggestible. As mentioned in the other answers try using HashMap.

Comments

0

Yes, you can. When you call

myArrayList.contains(myObejct);

the ArrayList will invode myObejct's equals method. So you can tell if the object is already in you list. And I think you can change you method a little,

@Override
public boolean equals(Object o){
    if (!(o instanceof YourClass))  
        return false;
    YourClass object = (YourClass)o;
    return this.getInt.equals(object.getInt());
}

because if you don't, the method "getInt" might cause a MethodNotFound exception.

Comments

0

Well, that is one way to approach the problem.

  • Your equals will probably work provided that you change Object object = (Object)o; to cast to the real class.

  • However, equals ought to cope with the case where o is not of the expected type. The contract requires you should return false rather than throwing a ClassCastException ...

You would then use list.contains(o) to test if an object with the same int value exists in the list. For example:

    if (!list.contains(o)) {
        list.add(o);
    }

But when you override equals, it is best practice to also override hashcode ... so that your class continues to satisfy the equals / hashcode invariants. (If you neglect to do that, hash-based data structures will break for your class.)


However, this won't scale well, because the contains operation on an ArrayList has to test each element in the list, one at a time. As the list gets longer, the contains call takes longer ... in direct proportion; i.e. O(N) ... using Big O complexity notation.

So it may be better to use a Set implementation of some kind instead on ArrayList. Fepending on which set implementation you choose, you will get complexity of O(1) or O(logN). But the catch is that you will either have to to implement hashcode (for a HashSet or LinkedHashSet), or implement either Comparable or a Comparator (for a TreeSet).

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.