[closed]
@kiraSally
The 2017-07-21 though
Number of words2244
reading7

HashSet is pass

JAVA COLLECTIONS source


1. What is a HashSet

  • At the bottom, elements are stored using a HashMap
  • Non-heavy, out-of-order, out of sync
  • This version is based on JDK1.7

2. Data structure of HashSet

  • The class definition
      
  1. public class HashSet<E>
  2. extends AbstractSet<E>
  3. implements Set<E>, Cloneable. java.io.Serializable
  • Important global variable
      
  1. // Maintain a HashMap as the underlying data structure
  2. private transient HashMap<E.Object> map;
  3. Dummy Value, used as the default value of the underlying map when the HashSet is stored
  4. private static final Object PRESENT = new Object(a);
  • The constructor
      
  1. / * *
  2. * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  3. * default initial capacity (16) and load factor (0.75).
  4. * /
  5. public HashSet(a) {
  6. map = new HashMap< > ();
  7. }
  8. / * *
  9. * Constructs a new set containing elements from the specified collection.
  10. * The actual underlying layer creates a HashMap using the default load factor of 0.75 and an initial capacity large enough to contain all elements in the specified collection.
  11. * @param c where elements will be stored in collection in this set.
  12. * /
  13. public HashSet(Collection<? extends E> c) {
  14. map = new HashMap<E.Object> (Math.max((int) (c.size(a) /.75f) + 1. 16));
  15. addAll(c);
  16. }
  17. / * *
  18. Construct an empty HashSet with the specified initialCapacity and loadFactor.
  19. * The underlying layer actually constructs an empty HashMap with the corresponding parameters.
  20. * @param initialCapacity initialCapacity.
  21. * @param loadFactor Loads the factor.
  22. * /
  23. public HashSet(int initialCapacity. float loadFactor) {
  24. map = new HashMap< > (initialCapacity. loadFactor);
  25. }
  26. / * *
  27. * Construct an empty HashSet with the specified initialCapacity.
  28. * the actual underlying construct is an empty HashMap with the corresponding argument and loadFactor loadFactor of 0.75.
  29. * @param initialCapacity initialCapacity.
  30. * /
  31. public HashSet(int initialCapacity) {
  32. map = new HashMap< > (initialCapacity);
  33. }
  34. / * *
  35. * Constructs a new null link hash set with the specified initialCapacity and loadFactor. This constructor is package access, is not public, and is really just support for LinkedHashSet. The underlying implementation is to construct an empty LinkedHashMap instance with the specified parameters.
  36. * @param initialCapacity initialCapacity.
  37. * @param loadFactor Loads the factor.
  38. * @param dummy flag.
  39. * /
  40. HashSet(int initialCapacity. float loadFactor. boolean dummy) {
  41. map = new LinkedHashMap< > (initialCapacity. loadFactor);
  42. }

3. The HashSet storage

  • Add method parsing
      
  1. / * *
  2. * Use HashMap as the underlying data structure
  3. * 1. Implement non-duplicate key in HashMap
  4. * 2. The HashSet is not ordered because the HashMap is not ordered
  5. * 3. The HashMap value uses a final static variable as the default value
  6. * @return Return true if put succeeds, false otherwise (this method returns null when adding non-duplicate key pairs)
  7. * /
  8. public boolean add(E e) {
  9. return map.put(e. PRESENT) = =null;
  10. }
  • Analysis of Clone Method
      
  1. / * *
  2. * Returns a shallow copy of this HashSet instance: the elements themselves are not copied
  3. * @return The clone() method of HashMap is actually called to get a shallow copy of the HashMap
  4. * develop:
  5. * 1. Shallow copy is copying only the value type members of the class
  6. * 2. A deep copy is a copy of a class's value type members and reference type members
  7. * /
  8. public Object clone(a) {
  9. try {
  10. HashSet<E> newSet = (HashSet<E>) super.clone(a);
  11. newSet.map = (HashMap<E. Object>) map.clone(a);
  12. return newSet;
  13. } catch (CloneNotSupportedException e) {
  14. throw new InternalError(a);
  15. }
  16. }
  • An iterative approach
      
  1. / * *
  2. * 1. Iterator Iterator ()
  3. * 2. Iterate over the map keySet
  4. * /
  5. public Iterator<E> iterator(a) {
  6. return map.keySet().iterator(a);
  7. }


  • inductive


    • For objects saved in the HashSet, be careful to overwrite them properlyequalshashCodeMethod to ensure the uniqueness of the objects you put in

    • Ordered optionalLinkedHashSet.TreeSet

    • Thread safety is optionalCopyOnWriteArraySet.ConcurrentSkipListSet

  • Table of contents


      • COLLECTIONS 3
      • HashTable is pass
      • HashSet is pass
      • HashMap is pass
      • JAVA 3
      • HashTable is pass
      • HashSet is pass
      • HashMap is pass
      • The source code 3
      • HashTable is pass
      • HashSet is pass
      • HashMap is pass
    • The following [tags] will be used to mark this contribution:
    • Downloading a Client
    • Focus on developers
    • Report problems and suggestions
    • Contact us

Add a new annotation
Save the cancel
This annotation is viewable only to you and the author until the author makes it public.

Save the cancel

Modify save cancel delete

  • private
  • public
  • delete

Check out the five earlier replies

Respond to comments