I know both the IdentityHashSet (via Collections#newSetFromMap(Map))and the LinkedHashSet. However, what I need is a combination of the two, a LinkedIdentityHashSet. I could not find any ready-made solution on the net. Anybody knows how to tweak this?
Thanks for advice!
The implementation techniques there don't marry up very well. LinkedHashMap adds linked listing to the entry objects of the map. IdentityHashMap uses a probing technique and so avoids having any entry objects.
There's a few ways to add "identity" characteristics to a collection/map.
final equals and hashCode methods. Really all reference-type types should have this, but never mind.equals and hashCode but can modify the class, add a final field that is of a final class that holds a final references to the type you were going to use as the key. Use the new field as the key.equals/hashCode methods to call ==/System.identityHashCode on the original object. One option is to use LinkedHashSet and a wrapper
class LinkedIdentityHashSet<E> extends AbstractSet<E> {
    Set set = new LinkedHashSet();
    static class IdentityWrapper {
        Object obj;
        IdentityWrapper(Object obj) {
            this.obj = obj;
        }
        public boolean equals(Object obj) {
            return this.obj == obj;
        }
        public int hashCode() {
            return System.identityHashCode(obj);
        }
    }
    public boolean add(E e) {
        return set.add(new IdentityWrapper(e));
    }
...
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With