Sunday, August 14, 2016

HashSet Quick Reference

  • Backed by a hash table (actually a HashMap instance)
  • While iterating HashSet the order of elements are not guaranteed
  • Offers constant time performance for the basic operations add, remove, contains and size
  • In multi-threaded environment, to prevent accidental unsynchronized access to the HashSet that can modify HashSet, wrap HashSet using Collections.synchronizedSet method

      Set s = Collections.synchronizedSet(new HashSet(...));


  • The iterators returned by this class's iterator methods are fail-fast. If the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.
  • Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. But one must not write program that depends on this behaviour.
  • iterator()method returns an iterator over the elements in this set. The elements are returned in no particular order.
  • boolean add(E e) method Adds the specified element e to this set if it is not already present and return true. If this set already contains the element e then the set remains unchanged and method returns false.
  • Hashset allows one null value

Total Pageviews