Last active
June 10, 2018 04:59
-
-
Save LeoHeo/d8dbcb11caac8441a898ae3234722ff8 to your computer and use it in GitHub Desktop.
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; | |
/** | |
* 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<>(); | |
} | |
/** | |
* Adds the specified element to this set if it is not already present. | |
* More formally, adds the specified element <tt>e</tt> to this set if | |
* this set contains no element <tt>e2</tt> such that | |
* <tt>(e==null ? e2==null : e.equals(e2))</tt>. | |
* If this set already contains the element, the call leaves the set | |
* unchanged and returns <tt>false</tt>. | |
* | |
* @param e element to be added to this set | |
* @return <tt>true</tt> if this set did not already contain the specified | |
* element | |
*/ | |
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