0

I have to add only unique elements to an ArrayList. How can I override the equals method for that purpose? I want something like:

public boolean equals (Object in) {
    if(in == null) {
        return false;
    }
    else if(in instanceof UniqueFruits) {
        // ?? check whether the newly added element exists or not           
        return true;
    }
    else {
        return false;
    }       
} 

How to check whether the newly added element exists or not? I have to check on the Fruit Names.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
  • You must override `equals` and `hashCode` methods in order to do this. – Luiggi Mendoza Aug 09 '12 at 04:00
  • But how? I need the code for the check part. – Nihal Sharma Aug 09 '12 at 04:02
  • You must compare the attribute values inside your `if( in instanceof UniqueFruits) { ... }` code to return true or false, depending on your rules, like fruit name or whatever you want/need – Luiggi Mendoza Aug 09 '12 at 04:03
  • 1
    Even once you get equals() working for your custom object, you'll still need extra logic to decide whether or not to add it to your ArrayList. If you need a unique collection, you can use one of the [Set](http://docs.oracle.com/javase/6/docs/api/java/util/Set.html) implementations. – Thomas Aug 09 '12 at 04:08

3 Answers3

2

It sounds like you want to use a Set implementation. If order doesn't matter, just use HashSet. If you want to keep insertion-order, use LinkedHashSet. Or, to maintain natural ordering, use TreeSet.

In any case, make sure to override your element object's equals method. What you have is a good start. After checking in instanceof UniqueFruits, cast in to UniqueFruits:

UniqueFruits uf = (UniqueFruits)in;

You can then check each relevant field using equals in turn (make sure to check for null first if a field is nullable). Any modern IDE will help you generate equals automatically. It may be educational to try it yourself first, then compare with the generated version.

Make sure to override hashCode also (IDEs will similarly help you with this, and there is plenty of reading online about the matter - just search).

If you use TreeSet (or some other SortedSet implementation), your element object should either implement Comparable or else you should provide the SortedSet with a Comparator.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1

I would suggest using Set instead of ArrayList for storing unique elements. It is easier and cleaner.

As per the javadoc for Set, here's what it says:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction

Here's an SO article that talks about how to remove repeated elements from ArrayList. However, you would notice that most of the solutions provided uses an implementation of Set to achieve what you want to do. In any case, over-riding equals() as you've done will not help achieve your objective. If it's not too much of a trouble (I don't understand why you need to stick with an ArrayList given your actual requirement, I would strongly suggest moving to a Set implementation.

Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49
0

If your fruitname class has 2 attributes, like fruitname and fruitclass then

your check code should be

UniqueFruits current = (UniqueFruits) in;
if(this.fruitname.equals(current.fruitname) && this.fruitclass.equals(current.fruitclass)) {
  return true;
}else{
   return false;
}

And coming to hashcode ask your IDE to generate the hasdcode for UniqueFruits Class. And use Set instead of ArrayList

On a lighter note Your IDE can even generate equals() for you.

Patton
  • 2,002
  • 3
  • 25
  • 36