Does a set has the same elements as a list in Java?

java, list, set

Solution

There are several collections libraries to do this. For example commons-collection: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEqualCollection-java.util.Collection-java.util.Collection-

eg. `CollectionUtils.isEqualCollection(myList, mySet)`

If you have to write it yourself, no libraries, then just check that each contains all the elements of the other:

`mySet.containsAll(myList) && myList.containsAll(mySet)`

Problem

I have an `ArrayList<SomeObject>` in java which contains some `<SomeObject>` multiple times. I also have a `Set<SomeObject>`, which contains some elements one time only. The elements are only uniquely distinguishable only by their name (`String SomeObject.Name`). How am I possible to see if the list has exactly the same elements as the set, but maybe multiple times? Thanks

Original source