-1

For example considering the list bellow:

List<MyCustomObject> list = Arrays.asList(new MyObject(1, "one")...);

I'd like to know how to check if the list contains duplicate items, considering the second parameter on the constructor that is a String.

Appleshell
  • 7,088
  • 6
  • 47
  • 96
  • `list.size() == new HashSet<>(list).size()`. – Andy Turner Apr 12 '18 at 20:16
  • 1
    @AndyTurner plus the hashCode and equals – Youcef LAIDANI Apr 12 '18 at 20:17
  • There are many, *many* ways to do that. The best choice would depend on what your `MyObject` looks like, where the data comes from and how, etc. – Kayaman Apr 12 '18 at 20:17
  • If you don't need to know what the duplicates are, then use Andy Turner's solution. If you do need to know what they are, then sort the list, and iterate through it comparing each item to the next. – Dawood ibn Kareem Apr 12 '18 at 20:22
  • Possible duplicate of [How to check if exists any duplicate in Java 8 Streams?](https://stackoverflow.com/questions/30053487/how-to-check-if-exists-any-duplicate-in-java-8-streams) – Ousmane D. Apr 12 '18 at 22:08

2 Answers2

1

First you need to have hashcode and equals implemented for MyObject. Then there are multiple ways of checking if the duplicates exists -

  1. Use what @AndyTurner suggested in the comment.
  2. Use - list.size() == list.stream().distinct().count()
  3. Use - list.stream().allMatch(new HashSet<>()::add)

You can check which one is better than other in terms of performance.

Anand Mattikopp
  • 332
  • 2
  • 11
0

A duplicates check is heavily dependent on the attributes of an object. If the check is being performed on an object then it's necessary to consider what parts of that object are unique and compare those values. However there are some downfalls to this approach, if the objects will likely have the same values then this approach becomes more difficult.

From past project experience, an ID tag is one of the easiest forms of duplicate detection to implement. When the object is initialized, assign a unique ID value so only the ID must be checked in a duplicate check.

A data structure of sorted objects makes the process a little easier because you know where the object should be, if it exists.

sellc
  • 380
  • 1
  • 5
  • 19