Created
February 15, 2014 13:10
-
-
Save jeevan-patil/9019149 to your computer and use it in GitHub Desktop.
Implementation of add method in java HashSet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HashSet<E> | |
extends AbstractSet<E> | |
implements Set<E>, Cloneable, java.io.Serializable | |
{ | |
private transient HashMap<E,Object> map; | |
// Dummy value to associate with an Object in the backing Map | |
private static final Object PRESENT = new Object(); | |
/** | |
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has | |
* default initial capacity (16) and load factor (0.75). | |
*/ | |
public HashSet() { | |
map = new HashMap<E,Object>(); | |
} | |
public boolean add(E e) { | |
return map.put(e, PRESENT)==null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment