A set in Java is a collection that does not contain duplicate elements, specifically an element pair that does not contain e1.equals (E2). Allows NULL to be added in set. Set does not guarantee the order of elements in the collection.
When an element is added to a set, if the specified element does not exist, the addition succeeds. That is, if the element E1 (E==null e1==null:e.queals (E1)) is not present in the set, E1 can be added to the set.
The following is an example of a implementation class hashset of Set, which briefly introduces the principle of set repeat implementation:
[Java] view plain copy print? Package com.darren.test.overide; public class CustomString {private String value; Public customstring () {This (""); Public customstring (String value) {this.value = value; } }
Package com.darren.test.overide;
public class CustomString {
private String value;
Public customstring () {This
("");
}
Public customstring (String value) {
this.value = value;
}
}
[Java] View Plain copy print? package com.darren.test.overide; import java.util.hashset; Import java.util.Set; public class hashsettest { public static void main (String[] args) { string a = new string ("a"); string b = new string ("A"); customstring c = new customstring ("B"); Customstring d = new customstring ("B"); system.out.println ("a.equals (b) == " + a.equals (b)); System.out.println ("C.equals (d) == " + c.equals (d)); set<object> set = new HashSet<Object> (); Set.add (a); set.add (b); set.add (c); Set.add (d); system.out.println ("Set.size () == " + set.size ()); for (object object : set) { System.out.println (object); } } }
Package com.darren.test.overide;
Import Java.util.HashSet;
Import Java.util.Set;
public class Hashsettest {public
static void Main (string[] args) {
string a = new string ("a");
String b = new String ("A");
CustomString C = new CustomString ("B");
CustomString d = new CustomString ("B");
System.out.println ("a.equals (b) = =" + a.equals (b));
System.out.println ("c.equals (d) = =" + c.equals (d));
set<object> set = new Hashset<object> ();
Set.add (a);
Set.add (b);
Set.add (c);
Set.add (d);
System.out.println ("set.size () = =" + set.size ());
For (object Object:set) {System.out.println (object);}}}
The results of the operation are as follows:
[plain] view plain copy print? A.equals (b) = = True C.equals (d) = = False Set.size () = = 3 Com.darren.test.overide.customstring@2c39d2 A Com.darre N.test.overide.customstring@5795ce
A.equals (b) = = True
c.equals (d) = = False
set.size () = = 3
com.darren.test.overide.customstring@2c39d2
A
Com.darren.test.overide.customstring@5795ce
Perhaps you have seen the key, yes, is the Equals method. It is not appropriate to say that it should be the Equals and Hashcode method. Why do you say that, let's change the CustomString class in the test:
[Java] View Plain copy print? package com.darren.test.overide; public class customstring { private String value; Public customstring () { this (""); } public customstring (String value) { this.value = value; } @Override public boolean equals (object obj) { if (this == obj) { return true; } else if (obj instanceof customstring) { CustomString customString = (customstring ) obj; return customstring.value.equals ( Value); } else { return false; } } }
Package com.darren.test.overide;
public class CustomString {
private String value;
Public customstring () {This
("");
}
Public customstring (String value) {
this.value = value;
}
@Override public
boolean equals (Object obj) {
if (this = = obj) {return
true;
} else if (obj instanceof customstring) {
customstring customstring = (customstring) obj;
return customString.value.equals (value);
} else {return
false;}}}
Test results:
[HTML] view plain copy print? A.equals (b) = = True C.equals (d) = = True Set.size () = = 3 Com.darren.test.overide.customstring@12504e0 A Com.darre N.test.overide.customstring@1630eb6
A.equals (b) = = True
c.equals (d) = = True
set.size () = 3
com.darren.test.overide.customstring@12504e0
A
Com.darren.test.overide.customstring@1630eb6
This time the Equals return value is true, but the set size is still 3
Let's keep changing.
[Java] View Plain copy print? package com.darren.test.overide; public class customstring { private String value; Public customstring () { this (""); } public customstring (String value) { this.value = value; } @Override public int hashcode () { // return super.hashcode (); return 1; } }
Package com.darren.test.overide;
public class CustomString {
private String value;
Public customstring () {This
("");
}
Public customstring (String value) {
this.value = value;
}
@Override public
int hashcode () {
//return Super.hashcode ();
return 1;
}
}
Then look at the results:
[plain] view plain copy print? A.equals (b) = = True C.equals (d) = = False Set.size () = = 3 com.darren.test.overide.customstring@1 Com.darren.test.ov Eride. customstring@1 A
A.equals (b) = = True
c.equals (d) = = False
set.size () = = 3
com.darren.test.overide.customstring@1
com.darren.test.overide.customstring@1
A
Overriding the Hashcode method only, not overriding the Equals method
and finally change it.
[Java] View Plain copy print? package com.darren.test.overide; public class customstring { private String value; Public customstring () { this (""); } public customstring (String value) { this.value = value; } @Override public boolean equals (object obj) { if (this == obj) { return true; } else if (obj instanceof customstring) { CustomString customstring = (customstring) obj; return customstring.value.equals (value); } else { return false; } } @Override public int hashcode () { // return super.hashcode (); return 1; } }
Package com.darren.test.overide;
public class CustomString {
private String value;
Public customstring () {This
("");
}
Public customstring (String value) {
this.value = value;
}
@Override public
boolean equals (Object obj) {
if (this = = obj) {return
true;
} else if (obj instanceof customstring) {
customstring customstring = (customstring) obj;
return customString.value.equals (value);
} else {return
false;
}
}
@Override public
int hashcode () {
//return Super.hashcode ();
return 1;
}
}
Final results:
[plain] view plain copy print? A.equals (b) = = True C.equals (d) = = True Set.size () = = 2 com.darren.test.overide.customstring@1 A
A.equals (b) = = True
c.equals (d) = = True
set.size () = = 2
com.darren.test.overide.customstring@1
A
OK, prove that you need to rewrite the Equals method and the Hashcode method to see the principle:
Java.lnag.Object of the hashcode in the contract:
1. During an application execution, if the information used to compare the Equals method of an object is not modified, the Hashcode method is called multiple times on the object, and it must consistently return the same integer.
2. If two objects are equal according to the Equals (object O) method, the Hashcode method that calls either of the two objects must produce the same integer result.
3. If two objects are not equal according to the Equals (object O) method, the Hashcode method of either of these two objects is called, and no different integer results are required. However, if they can be different, they may improve the performance of the hash table.
In HashSet, the basic operations are implemented at the HashMap level, because the hashset is stored in hashmap with the underlying data. When an element is added to the HashSet, the hashcode value of the element is computed first, and then the storage location of the element is computed using the (hashcode)% of the element (the size of the HashMap set) +1, if the position is empty, the element is added; The Equals method is used to compare whether the elements are equal, equality is not added, or an empty space is added.
The following is part of the source code of HashSet:
[Java] View Plain copy print? package java.util; public class hashset<e> extends abstractset<e> implements set<e>, cloneable, java.io.Serializable { static final long serialversionuid = -5024744406713321676l; // the bottom uses HashMap to save all elements in the hashset. private transient HashMap<E,Object> map; // Defines a virtual object as the value of HashMap, which defines this object as static final. private static final object present = new object (); /** &NBSP; * the default parameterless constructor, constructs an empty hashset. * * The actual bottom layer Initializes an empty HashMap and uses the default initial capacity of 16 and load factor 0.75. */ public HashSet () { map = new hashmap <E,Object> (); }