Home > Java > javaTutorial > A brief introduction to HashSet in Core Java

A brief introduction to HashSet in Core Java

黄舟
Release: 2017-09-19 11:43:19
Original
1893 people have browsed it

The following editor will bring you a Core Java article to briefly talk about HashSet (recommended). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

When students are reading this question, I will first ask two questions, and then everyone will understand better when reading this article with questions.

1. Why can’t HashSet add duplicate elements when adding elements?

#2. Does HashSet add null elements?

Open the source code, we see the following code, we see that HashSet also has a HashMap as an attribute, and the construction method of HashSet() is to instantiate this map. If you don’t know about HashMap, you can read this blog post of mine. Also note that there is a static final object PRESENT. What is this used for? Let’s continue reading.


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<>();
 }
Copy after login

Then we open its add method, which is to put the element e into the HashMap, and then put the static final object PRESENT into it as the value. If the addition is successful, Then HashMap returns null, which means the addition is successful. We also mentioned it in the previous blog post. Let’s talk about it again as a review. If the element is placed in a HashMap, its hashCode is first judged. If the hashCode is not found, the index is calculated based on the hashCode and placed in the corresponding bucket. If the hashCode is the same, then the second judgment is based on whether the key is equals and placed in the corresponding bucket. It's in the linked list.


/**
  * Adds the specified element to this set if it is not already present.
  * More formally, adds the specified element <tt>e</tt> to this set if
  * this set contains no element <tt>e2</tt> such that
  * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
  * If this set already contains the element, the call leaves the set
  * unchanged and returns <tt>false</tt>.
  *
  * @param e element to be added to this set
  * @return <tt>true</tt> if this set did not already contain the specified
  * element
  */
 public boolean add(E e) {
  return map.put(e, PRESENT)==null;
Copy after login

Of course, students have also thought of the second question, because hashMap supports null key, so HashSet can also add elements with null key. HashMap is used in so many places. Do you know how important it is? !

The above is the detailed content of A brief introduction to HashSet in Core Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template