-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.

5
  • Go look at the source code. Commented Jun 21, 2014 at 18:36
  • 4
    Yes, a HashSet is backed by a HashMap. The Javadoc even says so. You should peruse the Javadoc for these classes; it would answer most of these questions. Commented Jun 21, 2014 at 18:36
  • @GeorgeNetu: No, I don't see that as a duplicate question at all. Commented Jun 21, 2014 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. Commented Jun 21, 2014 at 18:42

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is correct, but it barely feels like an answer.
@Makoto Such is the problem with such questions :|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.