Setting an instance in Java?
Java defines a set interface, where contains() is defined as follows:
The collection interface defines contains() as follows:
I need a Java 'instance set', where contains () is based on = = instead of equals () In other words, a set of hard instances in which two different objects a and B, in which A. equals (b) can coexist in the same group because a= B.
Is such an 'instance set' provided in Java or in some public libraries? I can't find anything, but maybe someone knows better If not, I will implement it thank you.
Solution
There is no direct instance set in JRE
But there is an identityhashmap, which implements the "instance map" according to your terminology
And there is one called collections The method of newsetfrommap() can create a set from any map implementation
Therefore, you can easily build your own instance set, as follows:
Set<MyType> instanceSet = Collections.newSetFromMap(new IdentityHashMap<MyType,Boolean>());