Package COM. broadengate. fangxing; import Java. util. arraylist; import Java. util. collection; import Java. util. hashset;/*** it should be known that hashset is not allowed to be put into duplicate data. So what is it based on to determine whether it is a duplicate data? * Hash is used in hashset. Algorithm Because the hashset underlying layer also uses hashmap to access data *, while the hashmap underlying layer uses arrays to access data, and hashcode * is used to store data in hashmap *, this is the reason why the data stored by hashmap is not sequential. It is stored Based on hashcode. ** if hashcode is not overwritten, the object is used by default, it is a hashcode generated based on the object's reference address. Therefore, when no object is created, its reference address is different, **/public class hashcodetest {public static void main (string [] ARGs) {hashcodetest = new hashcodetest (); Collection coll = new/* arraylist () */hashset (); person = hashcodetest. New person ("1", 1); person person1 = hashcodetest. new person ("1", 1); person person2 = hashcodetest. new person ("1", 1); person person3 = hashcodetest. new person ("1", 1); person person4 = hashcodetest. new person ("1", 1); person person5 = hashcodetest. new person ("1", 1); Coll. add (person5); Coll. add (person4); Coll. add (person); Coll. add (person2); Coll. add (person3); Coll. add (person1); // If the hashcode and EQ methods are overwritten, if Change the value of the object in the memory, and the hashcode value will change. // The JVM cannot find the person object in the region calculated based on the hashcode, therefore, the person cannot be removed. setage (2); Coll. remove (person); system. out. println (Coll. size ();} class person {private string name; private int age; Public Person () {} public person (string name, int age) {This. name = Name; this. age = age;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getage () {re Turn age;} public void setage (INT age) {This. age = age;}/*** @ override public int hashcode () {final int prime = 31; int result = * 1; Result = prime * result + getoutertype (). hashcode (); * result = prime * result + age; Result = prime * result + * (name = NULL )? 0: Name. hashcode (); return result;} * @ override public Boolean equals (Object OBJ) {If (this = OBJ) return * true; If (OBJ = NULL) return false; If (getclass ()! = * Obj. getclass () return false; Person Other = (person) OBJ; * If (! Getoutertype (). Equals (other. getoutertype () return * false; If (age! = Other. Age) return false; If (name = * null) {If (other. Name! = NULL) return false;} else if *(! Name. Equals (other. Name) return false; return true;} ** private hashcodetest getoutertype () {return * hashcodetest. This ;}**/}}