user3777478
user3777478

Reputation: 11

Java (ArrayList check with object's int)

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());
}

Upvotes: 1

Views: 111

Answers (5)

Stephen C
Stephen C

Reputation: 719606

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).

Upvotes: 0

Snail
Snail

Reputation: 111

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.

Upvotes: 0

Rohit Kumar
Rohit Kumar

Reputation: 24

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.

Upvotes: 0

ag.albachicar
ag.albachicar

Reputation: 357

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);

Upvotes: 1

Makoto
Makoto

Reputation: 106508

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).

Upvotes: 3

Related Questions