Skip to content

Instantly share code, notes, and snippets.

@jeevan-patil
Created February 15, 2014 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeevan-patil/9019149 to your computer and use it in GitHub Desktop.
Save jeevan-patil/9019149 to your computer and use it in GitHub Desktop.
Implementation of add method in java HashSet.
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