Notes for using HashSet in java-15.3 from scratch
In this section, we will discuss the precautions for using various implementations of Set.
Common Implementation classes of the Set interface include HashSet, TreeSet, and LinkedHashSet.
1. HashSet
The impression of HashSet is that it can remove duplicate elements. Each element is unique, but there is a premise that the equals and hashcode methods must be rewritten.
Most of the impressions are as follows:
package com.ray.ch15;import java.util.HashSet;public class Test {public static void main(String[] args) {HashSet
set = new HashSet
();set.add(1);set.add(2);set.add(3);for (int i = 0; i < 10; i++) {set.add(i);}System.out.println(set);}}
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In fact, the reason is that the equals and hashcode methods in the set all inherit from the objects, and the two methods in the Object can easily compare the basic types of java, even most of the types in java, because most of the types have overwritten the equals and hashcode methods.
So let's take a look at the custom type below:
package com.ray.ch15;import java.lang.reflect.InvocationTargetException;import java.util.HashSet;import java.util.Set;public class Test
{public static
Set
fill(Set
set, Class
type)throws InstantiationException, IllegalAccessException,IllegalArgumentException, SecurityException,InvocationTargetException, NoSuchMethodException {for (int i = 0; i < 10; i++) {set.add(type.getConstructor(int.class).newInstance(i));}return set;}public static
void test(Set
set, Class
type)throws IllegalArgumentException, SecurityException,InstantiationException, IllegalAccessException,InvocationTargetException, NoSuchMethodException {fill(set, type);fill(set, type);fill(set, type);System.out.println(set);}public static void main(String[] args) throws IllegalArgumentException,SecurityException, InstantiationException, IllegalAccessException,InvocationTargetException, NoSuchMethodException {test(new HashSet
(), SetType.class);test(new HashSet
(), HashType.class);}}class SetType {private int id = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}public SetType(int i) {id = i;}@Overridepublic String toString() {return id + "";}@Overridepublic boolean equals(Object obj) {return obj instanceof SetType && (id == ((SetType) obj).id);}}class HashType extends SetType {public HashType(int i) {super(i);}@Overridepublic int hashCode() {return getId();}}
Output:
[6, 6, 3, 4, 9, 5, 2, 9, 1, 7, 5, 4, 1, 2, 9, 3, 7, 8, 8, 0, 2, 0, 4, 6, 7, 5, 0, 1, 3, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Explain the above Code:
(1) fill Method: adds data to the imported set through generics and reflection.
(2) test method: We fill data in the set multiple times to see if the set is de-duplicated.
(3) SetType: the original type, but the equals and toString methods are implemented simply. toString indicates the object through the output id.
(4) HashType: inherits SetType and implements the hashCode method.
Note:
(1) From the output, we can see that in the first test method of main, its output is a lot of object IDs, and there are many duplicates in it, this is because we didn't override the hashCode method. In the hashCode method of the Object, the hashcode returned by each Object is different, and the jvm determines that it is a different Object. Therefore, how many objects are added to the Object, the number of objects displayed.
(2) As explained above, the following HashType overrides the hashCode method and uses id as the standard. As we can see from the above Code, IDs are very likely to be repeated, so jvm will de-duplicate.
We can open the source code of HashSet and look at the add method:
private transient HashMap
map;public boolean add(E e) {return map.put(e, PRESENT)==null; }
The object is put in the map. We are opening the put Method of map:
public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); for (Entry
e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; }
It compares hash values. Therefore, to deduplicate A HashSet, you must override the equals and hashcode methods.
(3) because the values generated above are relatively small, the special nature of sorting is not revealed. When we increase the number of created objects, the sorting will be arranged in a certain order, instead of following our imagination. We will talk about this sort in a later chapter. The following is the sample code:
package com.ray.ch15;import java.lang.reflect.InvocationTargetException;import java.util.HashSet;import java.util.Set;public class Test
{public static
Set
fill(Set
set, Class
type)throws InstantiationException, IllegalAccessException,IllegalArgumentException, SecurityException,InvocationTargetException, NoSuchMethodException {for (int i = 0; i < 20; i++) {set.add(type.getConstructor(int.class).newInstance(i));}return set;}public static
void test(Set
set, Class
type)throws IllegalArgumentException, SecurityException,InstantiationException, IllegalAccessException,InvocationTargetException, NoSuchMethodException {fill(set, type);fill(set, type);fill(set, type);System.out.println(set);}public static void main(String[] args) throws IllegalArgumentException,SecurityException, InstantiationException, IllegalAccessException,InvocationTargetException, NoSuchMethodException {test(new HashSet
(), HashType.class);}}class SetType {private int id = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}public SetType(int i) {id = i;}@Overridepublic String toString() {return id + "";}@Overridepublic boolean equals(Object obj) {return obj instanceof SetType && (id == ((SetType) obj).id);}}class HashType extends SetType {public HashType(int i) {super(i);}@Overridepublic int hashCode() {return getId();}}
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18]
Summary: This section describes the notes for using HashSet.
This chapter is here. Thank you.