Package advancejava;
Import static java. Lang. system. out;
Import java. util. collection;
Import java. util. comparator;
Import java. util. hashset;
Import java. util. treeset;
Class My implements Comparator
{
@ Override
Public int compare (Object O1, object O2 ){
// Todo auto-generated method stub
Return 0;
}
}
Class person_hash implements comparable
{
Public string name;
Public int age;
Public person_hash (string name, int age)
{
This. Name = Name;
This. Age = age;
}
Public Boolean equals (Object OBJ)
{
Person_hash P = (person_hash) OBJ;
If (OBJ = NULL |! (P instanceof person_hash ))
Return false;
If (P. Name. Equals (this. Name) & (P. Age = This. Age ))
Return true;
Else
Return false;
}
Public int hashcode ()
{
Return name. hashcode () + age * 9;
}
Public int compareto (Object O ){
Return 1;
}
}
/*
* If the elements (hashset hashmap) stored in the hash table are saved
* Do not change the value of a member that may affect the hashcode result,
* If you change the hashcode value of the object after saving it
* This object is not the one you have saved, so it will not be used again,
* Internal leakage occurs due to time accumulation.
*
*
*/
Public class hashcodetest {
Public static void main (string ARGs [])
{
Collection Col = new hashset ();
Person_hash p1 = new person_hash ("boy", 22 );
Person_hash P2 = new person_hash ("boy", 22 );
Person_hash P3 = new person_hash ("boy", 22 );
Person_hash P4 = new person_hash ("boy", 22 );
Col. Add (P1 );
Col. Add (P2 );
Col. Add (P3 );
Col. Add (P4 );
Out. println (Col. Size (); // The output result is 1. For a set, the conditions for access are
// Return value, which has such a condition in the source code
// E. Hash = hash & (E. Key = k | key. Equals (E. Key ))
// Trytree ();
Memeryout ();
}
Public static void trytree ()
{
Collection Col = new treeset ();
Person_hash p1 = new person_hash ("boy", 22 );
Person_hash P2 = new person_hash ("boy", 22 );
Person_hash P3 = new person_hash ("boy", 22 );
Person_hash P4 = new person_hash ("boy", 22 );
Col. Add (P1 );
Col. Add (P2 );
Col. Add (P3 );
Col. Add (P4 );
Out. println (Col. Size (); // The result is 4. Because their comparator is always equal, they are all stored in the set.
// It can be seen that the access responsibility of an ordered set falls on the comparator regardless of hashcode.
// Is equal to equals? As long as comparator returns a value not equal to 0, it will be saved to the set.
}
Public static void memeryout ()
{
Collection set = new hashset ();
Person_hash p1 = new person_hash ("boy", 22 );
Person_hash P2 = new person_hash ("girl", 21 );
Person_hash P3 = new person_hash ("person", 222 );
Set. Add (P1 );
Set. Add (P2 );
Set. Add (P3 );
Out. println (set. Size (); // The current result is 3.
P1.name = "dog ";
Set. Remove (P1 );
Out. println (set. Size (); // The current result is 3, because we changed the name of P1 in front, that is, indirectly changed the hashcode value.
// While hash depends on hashcode when taking values, and we have changed its hashcode, so we cannot find it.
// Modify the generated P1 object, so:
// Do not modify the attributes that affect hashcode Calculation for objects already stored in the Set set. Otherwise
// Cause internal leakage. This is very important and cannot be checked out easily.
}
}