1

I'm having a hard time remove duplicate items from my ArrayList of custom objects.

Removing Duplicates

public static ArrayList<UnchainedRestaurant> removeDuplicates(ArrayList<UnchainedRestaurant> arraylist) {
        //remove any duplicates
        ArrayList<UnchainedRestaurant> noDuplicates = new ArrayList<>();
        Set<UnchainedRestaurant> setItems = new LinkedHashSet<UnchainedRestaurant>(arraylist);
        noDuplicates.addAll(setItems);
        return noDuplicates;
    } 

Custom Object equals()

public boolean equals(Object o) {
        UnchainedRestaurant r = (UnchainedRestaurant) o;
        String name1 = this.getName();
        String name2 = r.getName();

        name1 = Util.normalizeVenueName(name1);
        name2 = Util.normalizeVenueName(name2);

        if(name1.equals(name2)) {
            return true;
        } else return false;
    }

Output after removing duplicates

1. 786 Kebab & Curry
2. Marlow's Tavern
3. P.F. Chang's
4. Ted's Montana Grill
5. Which Wich? Superior Sandwiches
6. Niko Niko Sushi
7. Burger 21
8. Tin Lizzys Bar and Grille
9. Saigon Flavors
10. Firehouse Subs
11. Luigi's Pizza
12. Roya Mediterranean Restaurant and Tapas Bar
13. East Coast Wings & Grill
14. Provino's Italian Restaurant
15. Kani House
16. Chow Baby
17. Mimi's Cafe
18. Monterrey Mexican Restaurant
19. Panera Bread - Mall of Georgia
20. Umami Asian Cuisine
21. Parma Tavern
7. Burger 21 --------------------------------
23. Luigi's A Slice of Italy
4. Ted's Montana Grill --------------------------------
25. Tom + Chee - Buford
2. Marlow's Tavern --------------------------------
1. 786 Kebab & Curry
28. Sushi Niko Niko
13. East Coast Wings & Grill
3. P.F. Chang's --------------------------------
31. Turkish Kitchen
32. The Cheesecake Factory
17. Mimi's Cafe
34. Aha Sushi
15. Kani House
3. P.F. Chang's --------------------------------
37. Teavana
38. Williams-Sonoma
39. Great Wraps
40. Auntie Anne's Pretzels
1. 786 Kebab & Curry --------------------------------
42. Bruster's Real Ice Cream
43. Spencer Gifts
44. Pretzelmaker
45. Little Tokyo of Georgia Mall
46. Great American Cookies

Any tips as to why it's not actually removing duplicates? Or is it actually removing duplicates by replacing the duplicate with the original? Not sure what's going on here.

5
  • 4
    If you override equals, you'd need to override hashcode as well. Commented Sep 24, 2015 at 13:24
  • 1
    If you do what @AlexisC. said you can change it to a Set which will do it for you ! Commented Sep 24, 2015 at 13:25
  • @StackFlowed OP already uses a LinkedHashSet. Commented Sep 24, 2015 at 13:26
  • its better to use Set instead of List to get unique values... Commented Sep 24, 2015 at 13:27
  • 1
    @StackFlowed A Set does not do it for you. It uses equals and hashcode so that it can find whether the object is a duplicate or not (for hash set in particular). Commented Sep 24, 2015 at 13:36

1 Answer 1

2

You also need to override the hashcode method since this is used by the set to allocate elements such that if obj1.equals(obj2) == true then obj1.hashcode() == obj2.hashcode() must also be true.

Thus, if your equals is something like this:

public boolean equals(Object o) {
    UnchainedRestaurant r = (UnchainedRestaurant) o;
    String name1 = this.getName();
    String name2 = r.getName();

    name1 = Util.normalizeVenueName(name1);
    name2 = Util.normalizeVenueName(name2);

    if(name1.equals(name2)) {
        return true;
    } else return false;
}

The hashcode would look something like so:

@Override
public int hashcode() {
     return Util.normalizeVenueName(this.getName()).hashcode();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! You learn something new every day :) So it looks like my custom object takes the name variable into the hash value of the object, and the Set uses these hash codes to compare two objects?
@jaytj95: Yes, since you are considering two UnchainedRestaurant objects to be equal if their names are the same, then, the hash of these same objects should also be the same. Collections which have 0(1) read time usually use hashes internally to distribute the elements. This applies to Sets and Maps in the case for Java.

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.