vendredi 9 juillet 2021

Is HashSet a well-written class?

I'm trying to learn design patterns as good coding practices and I would like to know if a HashSet is considered a well-written class? eg:

To construct a LinkedHashSet we use the following constructor

public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
}

Which calls this one:

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
}

So there is a useless param, is it correct to do that?

Also, I see that LinkedHashSet extends HashSet

LinkedHashSet<E> extends HashSet 

and HashSet references LinkedHashSet in its code, why there is nothing like a compile time recursion there?

// Create backing HashMap
        map = (((HashSet<?>)this) instanceof LinkedHashSet ?
               new LinkedHashMap<E,Object>(capacity, loadFactor) :
               new HashMap<E,Object>(capacity, loadFactor));

Aucun commentaire:

Enregistrer un commentaire