mercredi 2 août 2017

HashSet implementation using HashMap

if we see the code of the HashSet in java :-

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
                private transient **HashMap<E,Object>** map;

internally it is maintaining a HashMap and while adding the values to HashMap the method do:-

 public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

Why 'HashSet' uses 'HashMap' ? Though the memory consumption by 'HashMap' is more than 'DynamicArray'.




Aucun commentaire:

Enregistrer un commentaire