当前位置:网站首页>HashSet implementation class
HashSet implementation class
2022-06-25 05:42:00 【Axyzstra】
adopt HashCode Determine whether an element exists , Do not add if present , Otherwise, add , To achieve uniqueness
Common methods
Modifier and Type | Method and Description |
---|---|
boolean | add(E e) Adds the specified element to this collection ( If it doesn't already exist ). |
void | clear() Remove all elements from this collection . |
Object | clone() Back here HashSet Shallow copy of instance : The element itself is not cloned . |
boolean | contains(Object o) If this collection contains the specified element , Then return to true . |
boolean | isEmpty() If this collection does not contain elements , Then return to true . |
Iterator<E> | iterator() Returns the iterator of the elements in this collection . |
boolean | remove(Object o) If there is , Delete the specified element from the collection . |
int | size() Returns the number of elements in this collection ( Its cardinal number ). |
Spliterator<E> | spliterator() Create... On elements in this collection *late-binding and The fault is fast * Spliterator . |
demonstration
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
HashSet<String> test = new HashSet<>();
test.add(" Huawei ");
test.add(" Apple ");
test.add(" millet ");
System.out.println(test.size());
System.out.println(test);
// Adding duplicate elements does not add
test.add(" Huawei ");
System.out.println(test);
// Remove elements , because Set Set has no subscript , Only values can be specified for deletion
test.remove(" Huawei ");
System.out.println(test);
// Adding it again is unordered
test.add(" Huawei ");
// Traversal of the set
System.out.println("-----for------");
for (String string : test) {
System.out.println(string);
}
System.out.println("-----Iterator-----");
Iterator<String> it = test.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
rewrite hashcode
equals
Method
notes : These two methods can be overridden automatically by system generation
storage
- According to the
hashcode
Method to calculate the storage location , If there is no element in this position, it is stored directly , Otherwise, go to the next step - Re execution
equals
Method , If the return value istrue
The element is considered to be repeated ; Otherwise form a linked list
When the method is not overridden
import java.util.HashSet;
public class HashDemo {
private String name;
private int age;
public HashDemo(String name, int age) {
super();
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 age) {
this.age = age;
}
@Override
public String toString() {
return "HashDemo [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
HashSet<HashDemo> hash = new HashSet<>();
HashDemo h1 = new HashDemo(" Zhang San ", 20);
HashDemo h2 = new HashDemo(" Li Si ", 21);
HashDemo h3 = new HashDemo(" Wang Wu ", 22);
hash.add(h1);
hash.add(h2);
hash.add(h3);
System.out.println(hash.size());
System.out.println(hash);
// Adding the same object will be equals Judge as true, This object will not be added again
hash.add(h1);
System.out.println(hash.size());
System.out.println(hash);
// But for different objects with the same value , But can add
hash.add(new HashDemo(" Zhang San ", 20));
System.out.println(hash.size());
System.out.println(hash);
}
}
Before overriding the method , Different objects for the same value can still be added , This does not conform to the law of non repetition , So rewrite the method , The rewritten code is as follows
Only right hanshcode
The rewritten code is as follows
import java.util.HashSet;
public class HashDemo {
private String name;
private int age;
public HashDemo(String name, int age) {
super();
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 age) {
this.age = age;
}
@Override
public String toString() {
return "HashDemo [name=" + name + ", age=" + age + "]";
}
// rewrite hashcode Method makes hashcode Only related to name and age
@Override
public int hashCode() {
int n1 = this.name.hashCode();
int n2 = this.age;
return n1 + n2;
}
public static void main(String[] args) {
HashSet<HashDemo> hash = new HashSet<>();
HashDemo h1 = new HashDemo(" Zhang San ", 20);
HashDemo h2 = new HashDemo(" Li Si ", 21);
HashDemo h3 = new HashDemo(" Wang Wu ", 22);
hash.add(h1);
hash.add(h2);
hash.add(h3);
System.out.println(hash.size());
System.out.println(hash);
// Adding the same object will be equals Judge as true, This object will not be added again
hash.add(h1);
System.out.println(hash.size());
System.out.println(hash);
// But for different objects with the same value , But can add
hash.add(new HashDemo(" Zhang San ", 20));
System.out.println(hash.size());
System.out.println(hash);
}
}
3
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
3
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
4
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
You can still add elements with the same value , But not rewritten hashcode
The difference is , The newly added element is not placed in the array , Because of its with p1
Have the same hashcode
So new elements and p1
Occupy the same location , Just form a linked list for storage
Pictured
For not adding elements with the same value , Therefore, on the basis of the above, it is necessary to equals
Rewrite
import java.util.HashSet;
public class HashDemo {
private String name;
private int age;
public HashDemo(String name, int age) {
super();
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 age) {
this.age = age;
}
@Override
public String toString() {
return "HashDemo [name=" + name + ", age=" + age + "]";
}
// rewrite hashcode Method makes hashcode Only related to name and age
@Override
public int hashCode() {
int n1 = this.name.hashCode();
int n2 = this.age;
return n1 + n2;
}
// rewrite equals, Returns... When the values are equal true
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(this == obj) {
return true;
}
if(obj instanceof HashDemo) {
HashDemo h = (HashDemo)obj;
if(this.name.equals(h.name) && this.age == h.age) {
return true;
} else {
return false;
}
}
return false;
}
public static void main(String[] args) {
HashSet<HashDemo> hash = new HashSet<>();
HashDemo h1 = new HashDemo(" Zhang San ", 20);
HashDemo h2 = new HashDemo(" Li Si ", 21);
HashDemo h3 = new HashDemo(" Wang Wu ", 22);
hash.add(h1);
hash.add(h2);
hash.add(h3);
System.out.println(hash.size());
System.out.println(hash);
// Adding the same object will be equals Judge as true, This object will not be added again
hash.add(h1);
System.out.println(hash.size());
System.out.println(hash);
// But for different objects with the same value , But can add
hash.add(new HashDemo(" Zhang San ", 20));
System.out.println(hash.size());
System.out.println(hash);
//hash.remove(new HashDemo(" Zhang San ", 20)); rewrite equals You can delete in this way
}
}
3
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
3
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
3
[HashDemo [name= Wang Wu , age=22], HashDemo [name= Zhang San , age=20], HashDemo [name= Li Si , age=21]]
so , The same element will not be added at this time ( The same for people ).
边栏推荐
- Duplicate symbols for architecture i386 clang
- HR took the initiative to raise the salary of the test lady. How did she do it?
- 1.5.3 use tcpdump to observe ARP communication process
- JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes
- Stack and Queue
- How to choose the years of investment in financial products?
- JS function to realize simple calculator
- 2022.1.23 diary
- First blog
- Jenkins installation and configuration
猜你喜欢
Enhanced paste quill editor
Read the general components of antd source code
Classic usage of the sumproduct function
Monkey test of APP automation
UVA816 Abbott’s Revenge
JSON Library Tutorial from scratch (I): starting to learn and organize notes
Instant messaging project (I)
05 virtual machine stack
Example of dynamic programming 3 leetcode 55
Essais de pénétration - sujets d'autorisation
随机推荐
Example of dynamic programming 3 leetcode 55
渗透测试-提权专题
Small sample learning data set
Deep analysis of epoll reactor code
What is flush software? Is it safe to open an account online?
Uva1103 ancient pictograph recognition
ERDAS 9.2 installation tutorial
Interface learning
1.6.3 use tcpdump to observe DNS communication process
Only these four instructions are required to operate SQL data
Instant messaging project (I)
Dynamic programming full backpack
hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?
Deep learning non local neural networks
A method of automatic continuation of previous tables in word table
Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
Enhanced paste quill editor
Go Concurrency
What happens when you use "-fno exceptions", "new T"- With “-fno-exceptions”, what happens with “new T”?
[OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30