The source code analysis of HashSet is actually one thing.

Source: Internet
Author: User
Tags addall

The source code analysis of HashSet is actually one thing.

In the previous article, "HashMap is actually the source code analysis" introduces hashMap. This time we will show you the HashSet. HashSet is actually implemented based on HashMap. Therefore, we are familiar with HashMap, let's take a look at the source code of HashSet, which is extremely simple. Let's look at the source code directly below:

Public class HashSet <E> extends AbstractSet <E> implements Set <E>, Cloneable, java. io. Serializable {static final long serialVersionUID =-5024744406713321676L; // HashMap? Yes, HashSet stores data through HashMap. The value of HashSet is the key private transient HashMap <E, Object> map of HashMap; // HashMap is the key-value pair of <key, value>, now that the value of HashSet is the key of HashMap, the value of HashMap is, of course, the PRESENT. private static final Object PRESENT = new Object (); // The following constructor creates a HashMap. We have already introduced HashMap. We will not detail public HashSet () {map = new HashMap <> ();} here ();} // convert a known collection to a HashSet public HashSet (Collection <? Extends E> c) {// Why is the HashMap parameter written here? // The last time we introduced HashMap, we can see that if the capacity of HashMap is not specified, the default value is 16 // according to threshold = capacity * loadFactor, you can calculate capacity // Math. max (int) (c. size ()/. 75f) + 1, 16) This means that if capacity does not exceed 16, the default 16 map = new HashMap <> (Math. max (int) (c. size ()/. 75f) + 1, 16); // The method for converting a known collection to a HashSet // The addAll method is the method of the HashSet parent class AbstractCollection. For ease of reading, the code will be pasted below addAll (c);} public HashSet (int initialCapacity, float loadFac Tor) {map = new HashMap <> (initialCapacity, loadFactor);} public HashSet (int initialCapacity) {map = new HashMap <> (initialCapacity);} HashSet (int initialCapacity, float loadFactor, boolean dummy) {map = new LinkedHashMap <> (initialCapacity, loadFactor);} // The addAll method is the public boolean addAll (Collection <? Extends E> c) {boolean modified = false; for (E: c) // The add method here is rewritten by HashSet to implement if (add (e) modified = true; return modified;} // The core method of HashSet is coming. That's right. It's so simple public boolean add (E e) {// verify that the key mentioned above is the HashSet value return map. put (e, PRESENT) = null;} // the rest of these methods are related to Map. As long as you are familiar with HashMap, it is too simple, public boolean remove (Object o) {return map. remove (o) = PRESENT;} public void clear () {map. clear ();}}

In this way, the source code of HashSet is so simple. The following is a summary of the source code of HashSet:

1. HashSet is implemented based on HashMap and uses the value of HashSet as a key of HashMap, and an Object constant as the value of HashMap.

2. According to the characteristics of HashMap, we can infer that HashSet allows one null value, and the value of HashSet cannot be repeated.

3. When creating a HashSet, if appropriate, it is best to specify the capacity and loadFactory values of its internal HashMap. As for the reason, I mentioned it when introducing HashMap.

 

Okay. After talking about HashSet, I think it's time to raise this question: maybe when you are a beginner in java, I suggest you rewrite the object equals in the teacher or book, I 'd better rewrite the hashCode method. Remember? Why? Here is a demo:

First define a Person class:

Public class Person {// ID card private String idCard; private String name; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getIdCard () {return idCard;} public void setIdCard (String idCard) {this. idCard = idCard;} // rewrite the equals method (rule: consistent idCard, the same person is considered) @ Override public boolean equals (Object obj) {if (obj = this) {return true;} if (! (Obj instanceof Person) {return false;} Person others = (Person) obj; if (others. getIdCard (). equals (idCard) {return true;} return false ;}}

Then, write a test class and use HashSet to add the Person instance:

public class Test {    public static void main(String[] args) {                Person p1 = new Person();        p1.setIdCard("1234567890");                Person p2 = new Person();        p2.setIdCard("1234567890");                Set<Person> hashSet = new HashSet<Person>();        hashSet.add(p1);        hashSet.add(p2);                System.out.println(hashSet.size());            }    }

We know that the elements of HashSet cannot be repeated. Therefore, in the above test code, the p1 and p2 objects are equals. We originally wanted HashSet to save only one of the objects. However, this is counterproductive, the output result is 2, indicating that hashSet saves both objects. Why? Let's take a look at HashMap. First, because we didn't rewrite the hashCode method of Person, the hash values of p1 and p2 are inconsistent, hashMap places elements with inconsistent hash values in different locations, so two objects are generated. So how can we improve it? Of course, the hashCode method is rewritten. Next, we will rewrite the hashCode method in the Person class:

@Override    public int hashCode() {        return this.idCard.hashCode() * 11;    }

At this time, we use the test class above to test and find that the output is 1. OK. We finally got the same idea. This is one of the reasons why we strongly recommend that you rewrite the hashCode method while rewriting the equals method.

Now, this is the time to write. Thank you!

 


The add method of the Set interface. If there is an interpolation, false is returned. Why does HashSet not return false to an existing one ......

Set <String> set = new HashSet <String> (); In the while LOOP
Each input also adds a new HashSet instance.
Set is constantly refreshed to point to new objects,
So there will never be repeated input images, because there will always be only one data in the set.

Question about java hashset

HashSet does not allow repeated elements. Therefore, repeated strings in your String array will not be added to HashSet.
Code Description:
Traverse your arrays and add them to the HashSet one by one. Then, take out the elements in the HashSet and assign them to the String array.
The length of the array is shortened. are repeated strings deleted.
The warning is okay. It's a List generic warning. For example, List list = new ArrayList (); now IDE will report a warning to add a generic type, that is, to put the element type in List. for example, List <Integer> list = new ArrayList <Integer> ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.