I. INTRODUCTION
HashSet is a common subclass object for set, which implements the set interface, which is supported by a hash table (actually a hashmap instance). There is no guarantee for the iteration order of the set; In particular, it does not guarantee that orders will remain unchanged for a period of time. This class allows null elements.
This class provides a fixed time performance for basic operations (add,remove,contains and size), assuming that the hash function will scatter the correct elements in the bucket. Iterating over this collection requires a time proportional to the sum of the size (number of elements) of the HashSet instance and the "capacity" of the background HashMap instance (number of buckets). Therefore, if iterative performance is important, it is important not to set the initial capacity too high (or the load factor is too low).
The set set is unordered, and when stored, the hash table is stored in the order of the hashes, so-called unordered means that the order in which they are stored may be different from the order in which you join. When the hash address value of both objects is the same, the hash table is stored in the first to determine whether it is the same object, if the same object is not repeated storage, if judged to be different objects are deferred below it instead of right.
Two. Common Method description
Import Java.util.hashset;import Java.util.iterator;public class Set_1 {public static void main (string[] args) { Generate HashSet Object HashSet hashset1 = new HashSet (); add Element Boolean B1 = Hashset1.add ("111"); There is no repetition before adding this element, so the return value is True Hashset1.add ("22"); Hashset1.add ("333"); Hashset1.add ("1"); Boolean b2 = Hashset1.add ("111"); System.out.println ("b1=" +b1+ "; b2=" +b2); This element already exists in the collection, and the result of the returned value after adding is False String arry1[] = {"Arry1", "Arry2", "Arry3"}; int arry2[] = {11,22,3,44}; HashSet Hashset2 = new HashSet (); Hashset1.add (Arry1); Hashset1.add (Arry2); Hashset2.add ("You"); Hashset2.add ("is"); Hashset2.add ("yourself"); Add a collection Hashset1.addall (Hashset2) using AddAll; System.out.println (HASHSET1); Unordered, the order of withdrawal may differ from the order in which it was deposited, but the order of each fetch is the same (do not consider the order of each fetch to be different), System.out.println ("--------------------- Start iterator--------------------------"); Iterator loops out the elements in the HashSet Iterator it1 = Hashset1.iterator (); Using while, loop gets each element in the iterator//while (It1.hasnext ()) {//System.out.println (It1.next ());//}//Make With for, loop gets each element in the iterator System.out.println ("it1.hasnext () =" +it1.hasnext ()); for (; It1.hasnext ();) {System.out.println (It1.next ()); } }}
Cond
Introduction to examples of common methods of hashset in Java collections