当前位置:网站首页>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 TypeMethod and Description
booleanadd(E e) Adds the specified element to this collection ( If it doesn't already exist ).
voidclear() Remove all elements from this collection .
Objectclone() Back here HashSet Shallow copy of instance : The element itself is not cloned .
booleancontains(Object o) If this collection contains the specified element , Then return to true .
booleanisEmpty() If this collection does not contain elements , Then return to true .
Iterator<E>iterator() Returns the iterator of the elements in this collection .
booleanremove(Object o) If there is , Delete the specified element from the collection .
intsize() 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

  1. 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
  2. Re execution equals Method , If the return value is true The element is considered to be repeated ; Otherwise form a linked list

image-20220120155908304

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

image-20220120202719283

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 ).

原网站

版权声明
本文为[Axyzstra]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210501041704.html