Wrapper class for adding elements to HashSet I have many object of class Test. I want to be sure that among them, there are no two objects o1 and o2 where o1==o2. In order to achieve that, I want to add them all to a HashSet and check if hashSet.size() == numberOfInitialObjects . 

In order to ensure that the potential future changes in equals method of the  Test class will not affect my implementation, I wrote a simple wrapper so that instead of adding Test objects to the HashSet, the wrapper objects will be used. The code as such:

private class HashSetWrapper {

        private final Test component;

        public HashSetWrapper(Test component) {
            this.component = component;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 89 * hash + Objects.hashCode(this.component);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final HashSetWrapper other = (HashSetWrapper) obj;
            return this.component == other.component;
        }
    }


Is my approach OK, or are there more suitable ways to achieve my goal?
Why build your own implementation when you can use the features available in the Java standard libraries?

IdentityHashMap gives you all the features you need, and a Java Stream/collector will allow you to extract the map easily from your collection....

Collection&lt;Test&gt; tests = .....
Map&lt;Test,Test&gt; uniques = tests.stream().collect(Collectors.toMap(
     t -&gt; t,
     t -&gt; t,
     (t,u) -&gt; t,
     IdentityHashMap::new))


Now, if the size of the uniques is the same as the tests, you're good. The uniques Map is an IdentityHashMap where the key values are all identical instances (using ==). It supports a null key/value so that should be fine too.