Hashset<e> generic class

Source: Internet
Author: User

hashset<e> generic classes in the data organization similar to the mathematical set, you can do "intersection", "and", "poor" and other operations.

1. Hashset<e> Object

The objects created by the Hashset<e> generic class are called collections, such as:

<span style= "Font-family:microsoft yahei;font-size:14px;" >HashSet<String> set = hashset<string>; </span>


Then set is a collection of data that can be stored in string type, and set can call the Add (string s) method to add data of type string to the collection. The data that is added to the collection is called the element of the collection. The collection does not allow the same element, that is, if B is already an element in the collection, then executing the Set.add (b) operation is not valid. The initial capacity of the collection object is a 16-byte loading factor of 0.75. That is, if the collection adds more elements than 75% of the total capacity, the collection will increase its capacity by 1 time times.

2. Common methods

Common methods for hashset<e> generic classes are as follows:

①public boolean Add (E O)--Adds the element specified by the parameter to the collection.

②public void Clear ()-Clears the collection so that the collection does not contain any elements.

③public Boolean contains (Object O)--Determines whether the data specified by the parameter belongs to the collection.

④public Boolean IsEmpty ()--Determines whether the collection is empty.

⑤public boolean remove (Object o)--Sets the element specified by the delete parameter.

⑥public int Size ()--Returns the number of elements in the collection.

⑦object [] ToArray ()--stores the collection elements in the array and returns the array.

⑧boolean Containsall (HashSet set)--Determines whether the current collection contains the set specified by the parameter.

⑨public Object Clone ()--Gets a clone of the current collection that changes the elements in the object without affecting the elements in the current collection, and vice versa. 、

We can iterate through the collection with the help of the generic class Iterator<e>, a collection object that can use the iterator () method to return an object of type iterator<e>, if the collection is a collection of "student types", That is, the element in the collection is an object created by the Student class, and the collection uses the iterator () method to put back an object of type iterator<student> that uses the next () method to iterate through the collection.

<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "Font-family:microsoft yahei;font-size:14px;" >package com.chindroid.date;import java.util.hashset;import java.util.iterator;class Student{String name;int Score; Student (String name, int score) {this.name = Name;this.score = score;}} public class Testhashset {public static void main (string[] args) {Student zh = new Student ("Zhang Hongming", 77); Student wa = new Student ("Wangs", 68); Student li = new Student ("Li Jiajia", 67); hashset<student> set = new hashset<student> (); hashset<student> subset = new hashset<student> (); Set.add (en); Set.add (WA); Set.add (li); Subset.add (WA); Subset.add (LI), if (Set.contains (WA)) {System.out.println ("set contains:" +wa.name);} if (Set.containsall (subset)) {SYSTEM.OUT.PRINTLN ("collection set contains set subset");} int number = Subset.size (); System.out.println ("+number+" elements in the collection subset: "), Object s[] = Subset.toarray (); for (int i=0; i<s.length; i++) { System.out.printf ("Name:%s, score:%d\n", ((Student) s[i]). Name, ((Student) S[i]). score);} Number = Set.size (); System.out.println ("+" elements in Set set: ");iterator<student> iter = Set.iterator (); while (Iter.hasnext ()) {Student Te = Iter.next (); System.out.printf ("Name:%s, score:%d\n", Te.name,te.score);}} </span></span>


The program output results are as follows:
The collection set contains: Wangs Home
Collection set contains set subset
There are 2 elements in a collection subset:
Name: Wangs Home, Score: 68
Name: Li Jiajia, Score: 67
There is an element in the set set:
Name: Wangs Home, Score: 68
Name: Zhang Hongming, Score: 77
Name: Li Jiajia, Score: 67

3, the collection of the intersection, and with the difference

The collection object calls a Boolean AddAll (HashSet set) method that can be evaluated with the collection specified by the parameter, making the current collection the same as the two collection.

The collection object calls the Boolean Retainall (HashSet set) method to cross-operate the collection specified by the parameter so that the current collection becomes the intersection of two sets.

The collection object calls the Boolean RemoveAll (HashSet set) method to make the difference to the set specified by the parameter, making the current collection a difference of two sets.

The set parameter specifies that the collection must be of the same type as the current collection, otherwise the above method returns FALSE.

<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "Font-family:microsoft yahei;font-size:14px;" >package com.chindroid.date;import Java.util.hashset;import Java.util.iterator;public class TestHashset2 {public static void Main (string[] args) {integer one = new Integer (1), integer-n = new Integer (2), Integer three = new Integer (3); Integer four = new Integer (4), Integer five = new Integer (5), Integer six = new integer (6); hashset<integer> A = new hashset<integer> (); hashset<integer> B = new hashset<integer> (); hashset<integer> tempset = new hashset<integer> (); A.add (one); A.add (both); A.add (three); A.add (four); B.add (one); B.add (both); B.add (five); B.add (six); Tempset = (hashset<integer>) a.clone (); A.removeall (B); B.removeall (Tempset); B.addall (A); int number = B.size (); System.out.println (the symmetric difference set of "A and B" has "+ number +" elements: ");iterator<integer> iter = B.iterator (); while (Iter.hasnext ()) { Integer te = Iter.next (); System.out.printf ("%d,", TE. Intvalue ());}} </span></span>



The program output results are as follows:

The symmetric difference set of A and B has 4 elements:
3,4,5,6,

4, hashset<e> generic class implementation of the interface

The hashset<e> generic class implements the generic interface Set<e>, while the Set<e> interface is a sub-interface of the Collection<e> interface. Most of the methods in the Hashset<e> class are implementations of interface methods. When programming, you can use an interface callback technique that assigns a reference to an Hashset<e> object to an Collection<e> interface variable or set<e> interface variable, and the interface can invoke the implemented interface method.

Hashset<e> generic class

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.