-3

I have seen internal data structure used by hashset is HashTable on many websites but when i saw HashSet.class (after decompile) it is using HashMap.Now iam confused please clear my confusion.HashSet uses which data structure ?

Also tell me internal data structure used by linkedhashset, treeset, hashmap, hashtable, linkedhashmap, treemap.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Suraj Sharma
  • 89
  • 1
  • 5
  • Go look at the source code. – Sotirios Delimanolis Jun 21 '14 at 18:36
  • 4
    Yes, a `HashSet` is backed by a `HashMap`. [The Javadoc even says so.](http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html) You should peruse the Javadoc for these classes; it would answer most of these questions. – Makoto Jun 21 '14 at 18:36
  • @GeorgeNetu: No, I don't see that as a duplicate question at all. – Makoto Jun 21 '14 at 18:41
  • @Makoto I would agree that the *question* is, but the "I mean implementation wise.....?" in the question is ignored in most every answer so .. I don't think it's a "suitable" duplicate. – user2864740 Jun 21 '14 at 18:42
  • http://stackoverflow.com/questions/12431581/why-do-set-data-structures-in-java-use-map-internally?rq=1 , http://stackoverflow.com/questions/20217414/what-is-the-main-difference-between-hashset-treeset-and-linkedhashset-hashmap?rq=1 , http://stackoverflow.com/questions/11680751/are-there-problems-of-implementing-hashset-using-hashmap?rq=1 – user2864740 Jun 21 '14 at 18:43

1 Answers1

2

HashSet internally stores values as part of HashMap's key and it puts a dummy value as value

Check the source code, relevant portions extracted:

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
private transient HashMap<E,Object> map;

public boolean add(E e) { 
 return map.put(e, PRESENT)==null;
}
jmj
  • 237,923
  • 42
  • 401
  • 438