IdentitySet / IdentityHashSet (use IdentityHashMap)

You can use Guava's Sets.newIdentityHashSet().

https://github.com/google/guava/wiki/CollectionUtilitiesExplained


Make a set from a map

thx, @BoristheSpider

Collections.newSetFromMap(...), returns a Set backed by the specified map (in my case ➡️ IdentityHashMap):

// get IdentytitySet wich wrap IdentityHashMap
Set<Person> persons = Collections.newSetFromMap(new IdentityHashMap<>()) 

// test 
Person p  = new Person("a",1);
Person p2 = new Person("a",1);
persons.add(p);
persons.add(p2);
System.out.println(persons.toString());

stdout (pretty print):

[
  Person{
    s=StringProperty [value: a],
    d=DoubleProperty [value:1.0]
  }, 
  Person{
    s=StringProperty [value: a], 
    d=DoubleProperty [value: 1.0]
  }
]
p.setD(999);
persons.add(p);
System.out.println(persons.toString());

stdout (pretty print):

[
  Person{
    s=StringProperty [value: a],
    d=DoubleProperty [value: 999.0]
  }, 
  Person{
    s=StringProperty [value: a], 
    d=DoubleProperty [value: 1.0]
  }
]
persons.remove(p)
System.out.println(persons.toString());

stdout (pretty print):

[
  Person{
    s=StringProperty [value: a], 
    d=DoubleProperty [value: 1.0]
  }
]