HashSet: located under the Java.util package
1 HashSet Overview
Features: as a set set of one, the first is unordered, non-repeatable , allow the storage of NULL values, the underlying package hashmap, the implementation is not synchronized, the thread is not secure;
Common Constructs:
Public HashSet (): constructs a new empty set whose default initial capacity of the underlying HashMap instance is 16, and the load factor is 0.75.
Common Methods:
1) Add Features
Boolean add (e): if the specified element is not already contained in this set, the specified element is added
2) Judging function
Boolean IsEmpty (): returns trueif this set contains no Elements.
Boolean contains (Object o): returns true if the specified element is contained in this list
3) Get Features
int size (): Returns the number of elements in this set (capacity of set).
4) Delete function
Void clear (): removes all elements from this List. Note: This method is more violent and generally not used.
Boolean Remove (Object o): if the specified element exists in this set, it is Removed.
5) iterator function
Public iterator<e> Iterator () returns an iterator that iterates over the elements in this set. The order of the returned elements is not specific.
2 Use of HashSet
1) use HashSet to store strings : How to implement the uniqueness of storage elements??
Package settest;
Import java.util.HashSet;
Import java.util.Set;
public class HashSetDemo1 {
public static void main (string[] Args) {
set<string> set = new Hashset<string> ();
Set.add ("java");
Set.add ("js");
Set.add ("sql");
Set.add ("java");
Set.add ("js");
Set.add ("jquery");
For (String S:set) {
System.out.println (s);
}
}
}
Output result:
Java
Js
Jquery
Sql
Analysis:
1) by looking at the source code of the add () method, the underlying is using the HashMap put () method to implement the Element's access, continue to view the source of the put storage elements, to ensure that the uniqueness of the storage element depends on the implementation of the Equals and Hashcode methods of the element, First look at the hashcode method, if there is a hash value and the element to be stored equal hash value of the elements, then continue to compare the two by the Equals method. If they are equal, they are not stored.
2) because the string class overrides the Equals () and Hashcode () methods: both methods are dependent on the contents of the string object and are considered equal only to the contents of the string and are not stored in the Collection.
public int hashcode () {
int h = hash;
If (h = = 0 && value.length > 0) {
Char val[] = value;
for (int i = 0; i < value.length; i++) {
H = * H + val[i]; The generation of hash values depends on the contents of the string
}
hash = h;
}
Return h;
}
public boolean equals (Object Anobject) {
if (this = = Anobject) {
Return true;
}
If (anobject instanceof String) {
String anotherstring = (string) anobject;
int n = value.length;
if (n = = AnotherString.value.length) {
Char v1[] = value;
Char v2[] = anotherstring.value;
int i = 0;
While (n--! = 0) {//determines Whether two strings are equal, depending on whether the contents of the string are the same
If (v1[i]! = V2[i])
Return false;
i++;
}
Return true;
}
}
Return false;
}
It is also possible to store element uniqueness by storing the boxed types of other basic types, because these boxing types also implement the Equals () and Hashcode () methods. (E.G. integer,character, Etc.)
2) use HashSet to store custom objects: how to guarantee the uniqueness of the stored elements (here we think that Student's name and age equal means the elements are Equal)
Package settest;
public class Student {
Private String name;
private int age;
Public Student (String Name,int Age) {
THIS.name = name;
This.age = age;
}
Public String getName () {
Return name;
}
public void SetName (String Name) {
THIS.name = name;
}
public int Getage () {
Return age;
}
public void Setage (int.) {
This.age = age;
}
@Override
Public String toString () {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashcode () {
int hash = Name.hashcode () +age;
Return hash;
}
@Override
public boolean equals (Object Obj) {
If (this==obj)
Return true;
If (obj instanceof Student) {
Student stu = (Student) obj;
If (stu.getname (). equals (name) && stu.getage () ==age)
Return true;
}
Return false;
}
}
Package settest;
Import java.util.HashSet;
Import java.util.Set;
public class Hashsettest {
public static void main (string[] Args) {
set<student> set = new Hashset<student> ();
Student stu1 = new Student ("hu", 12);
Student stu2 = new Student ("wen", 27);
Student stu3 = new Student ("rt", 12);
Student stu4 = new Student ("you", 15);
Student stu5 = new Student ("hu", 12);
Student stu6 = new Student ("te", 17);
Student stu7 = new Student ("wen", 27);
Set.add (stu1);
Set.add (stu2);
Set.add (stu3);
Set.add (stu4);
Set.add (stu5);
Set.add (stu6);
Set.add (stu7);
For (Student S:set) {
System.out.println (s.getname () + "------" +s.getage ());
}
}
}
Output result:
Te------17
Hu------12
Wen------27
Rt------12
You------15
analysis:
From the output, the storage of this collection guarantees the uniqueness of the elements, mainly because we have overridden the equals and Hashcode methods in our custom Classes.
summary:
To realize the uniqueness of element access in the HashSet collection, because the underlying judgment relies on the Equals and hashcode methods, the elements that are accessed need to be rewritten in order to access the elements as we expect. Guarantees element Uniqueness.
HashSet of the Java collection