当前位置:网站首页>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 hashcodeequals Method
notes : These two methods can be overridden automatically by system generation
storage
- According to the
hashcodeMethod 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
equalsMethod , If the return value istrueThe 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 ).
边栏推荐
- Understand JS high-order function and write a high-order function
- Dynamic programming Backpack - 01 Backpack
- Professional things use professional people
- ERDAS 9.2 installation tutorial
- Voxel based and second network learning
- On Transform
- Use of pytorch tensorboard
- Electronic Society C language level 1 28, character diamond
- MySQL operation JSON
- Interface learning
猜你喜欢

1.6.3 use tcpdump to observe DNS communication process

Prototypical Networks for Few-shot Learning

Extend the toolbar of quill editor

CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati

Go implements LRU cache
Learn the interface test, see it is very good, and make a note

3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)

Essais de pénétration - sujets d'autorisation

滲透測試-提權專題

Five simple data types of JS
随机推荐
Dynamic programming example 2 leetcode62 unique paths
Small sample learning data set
Pytorch- daily learning notes of some small functions involving training
Dynamic programming example 1 leetcode 322 coin change
Basic bit operation symbols of C language
2.20 learning content
Jenkins installation and configuration
Personalized Federated Learning with Moreau Envelopes
Bind simulation, key points of interpreting bind handwritten code [details]
Baidu ueeditor set toolbar initial value
Deep learning non local neural networks
Design of IM login server and message server
Go Context - Cancelation and Propagation
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
Uva1103 ancient pictograph recognition
ERDAS 9.2 installation tutorial
Electric store stores data
Penetration test - right raising topic
投资理财产品的年限要如何选?
How to add an external header file in vs?