HashSet Hash set base for Java collections

Source: Internet
Author: User

HashSet implements the set interface, serializable, cloneable interface. Duplicate values are not allowed, but can be null and are not guaranteed to be consistent with the insertion order.

The time of the hashset iteration is proportional to the size of the HashSet instance (the number of elements) and the "capacity" of the underlying HashSet instance (the number of buckets). Therefore, if iterative performance is important, do not set the initial capacity too low and the load factor is too small. When the number of elements in the hash table is greater than the product of the current capacity and load factor, the expansion is about twice times the current capacity.

Where the data object must implement the Hashcode () function, the hashset thread is out of sync, using the Collections.synchronizedset wrapper.

constructor function:

HashSet () default initial size 16, reprint factor 0.75

HashSet (int initialcapacity)

HashSet (int initialcapacity,float loadfactor)

HashSet (Collection C)

Common methods:

Boolean Add (E E)

void Clear ()

Boolean contains (Object o)

Boolean remove (Object o)

Example:

Package test;

import java.util.*;

/**

* @author Administrator

*

*/

Public class Testhashset {

Public Static void Main (String args[]) {

hashset<string> hash=New hashset<string> ();

linkedlist<string> list=New linkedlist<string> ();

List.add ("List1");

List.add ("List2");

List.add ("List3");

Hash.add ("hash Set1");

Hash.add ("Hash Set2");

Hash.add ("hash Set3");

Hash.add ("hash Set4");

Hash.add ("hash Set5");

Hash.add ("hash Set6");

Hash.addall (list);//The list added to the hash set is not present as a separate element, [List1, Hash Set6, LIST3, List2, hash set3, hash set2, hash set5, hash set4, hash Set1]

System. out. println (hash);

Hash.remove ("hash Set3");

System. out. println (hash);

Iterator<String> it= hash.iterator ();

while (It.hasnext ()) {

System. out. println (It.next ());

}

}

}

HashSet is actually built on the basis of HashMap. The map's key is the set element, and the map value is constant.

Public Boolean Add (e e) {

return map. Put (E,PRESENT) = = null;

}

HashSet Hash set base for Java collections

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.