1. Set
When an object is added to the Set, the hashcode of the object needs to be added to the Set calculation. The hashcode is used to store the current object according to the value, when no object exists at this position, the set considers that the object does not exist in the set and adds it directly. If an object exists at this position, perform equals comparison between the object to be added to the set and the object at this position. If false is returned, a hash is performed, the new address calculated after the object is hashed. If true is returned, the object will not be added to the set.
2. When rewriting the equals method, you must override the hashcode method.
If the result of comparing two objects in a class with the equals method is true, the two objects must have the same hashcode
3. refer to the following code to run and view the output results to understand the above statements.
Test1 and test2 compare String. Because String overrides the equals and hashcode methods, String = "abc", and string = new String ("abc ") when the equals method is compared, true is returned. Therefore, the output results of test1 and test2 are the same. The Person class does not overwrite the equals method, while that of Person1, so the output results of test3 and test4 are different.
/*** Author Ding Chengyun * 2014-2-23 */package test; import java. util. hashSet; import java. util. iterator; import java. util. set;/*** @ author Ding Chengyun * 2014-2-23 */public class SetTest {/*** @ param args */public static void main (String [] args) {test4 ();} public static void test1 () {Set
S1 = new HashSet
(); S1.add ("abc"); s1.add ("xyz"); s1.add ("abc"); for (Iterator
Iter = s1.iterator (); iter. hasNext ();) {System. out. println (iter. next ();} // output: // abc // xyz} public static void test2 () {Set
S1 = new HashSet
(); S1.add (new String ("abc"); s1.add (new String ("xyz"); s1.add (new String ("abc"); for (Iterator
Iter = s1.iterator (); iter. hasNext ();) {System. out. println (iter. next ();} // output: // abc // xyz} public static void test3 () {Set
S = new HashSet
(); S. add (new Person ("zhangsan"); s. add (new Person ("lisi"); s. add (new Person ("zhangsan"); for (Iterator
Iter = s. iterator (); iter. hasNext ();) {System. out. println (iter. next (). getName ();} // output: // zhangsan // lisi} public static void test4 () {Set
S = new HashSet
(); S. add (new Person1 ("zhangsan"); s. add (new Person1 ("lisi"); s. add (new Person1 ("zhangsan"); for (Iterator
Iter = s. iterator (); iter. hasNext ();) {System. out. println (iter. next (). getName ();} // output: // lisi // zhangsan} class Person {String name; public Person (String name) {this. name = name;} public String getName () {return name;} class Person1 {String name; public Person1 (String name) {this. name = name;} public String getName () {return name;} public boolean equals (Object obj) {if (this = obj) {return true ;} if (obj instanceof Person1) {Person1 p = (Person1) obj; if (this. name. equals (p. getName () {return true;} return false;} public int hashCode () {return name. hashCode ();}}