First of all, we need to know that both the ArrayList and the HashSet classes are in the Java.util package, all of which implement the collection class, and collection is a standard.
We then introduce the differences between the two classes:
1, ArrayList: equivalent to a dynamic array, is a group of ordered collection When objects are added to arraylist that is, in this ordered set, each object is put into a reference, There may be multiple references to the same object, but there is no relationship
Package Com.internet;import Java.util.arraylist;public class Test {public static void main (string[] args) {ArrayList c = NE W ArrayList (); Integer a = new integer (1), Integer b = new Integer (1), C.add (a), C.add (b); System.out.println (C.size ()); Results: 2system.out.println (a.equals (b));//The result is true, you can see that two objects are equal}}
2, HashSet:when the object is placed, first see if there is an object in it (this determines whether the address of the object is the same), if there is, do not put, and if not, put it in:
Mytest.java
Package Com.internet;class mytest{int X;int y; Public MyTest (int x,int y) { this.x=x; this.y=y;} }
Reflecttest.java
Package Com.internet;import Java.util.hashset;public class Reflecttest {public static void main (string[] args) {HashSet h s = new HashSet (); MyTest T1 = new MyTest (1, 1); MyTest t2 = new MyTest (2, 2); MyTest t3 = new MyTest (1, 1); Hs.add (t1); Hs.add (T2); Hs.add (T3); Hs.add (t1); System.out.println (Hs.size ()); Results: 3}}
Here, one thing to note:in Flecttest.java, the T3 object is declared, where T3 and T1 are constructed with the same x, y values, but T1 and T3 are stored in the HS,
This is because the same object is said to refer to the memory address, not the value.
Secondly, immediately above the hashset example, if we need to let T1 and T3 is the same, after the T1 in the T2 can not be stored in, what should be done?
We need to override the Equals () method in Mytest.java:
Package Com.internet;import Java.util.*;class mytest1{int x;int y; Public MyTest1 (int x,int y) { this.x=x; this.y=y; } @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + X;result = Prime * result + Y;return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; MyTest1 other = (MyTest1) obj;if (x! = other.x) return false;if (Y! = other.y) return False;return true;}}
Then, the result in Flecttest.java is 2.
How to cover it specifically? Right-click on the Mytest.java Code window------> Find "Source" and click on-------> Find "Generate hashcode () and Equals ()" and click.
Also, after overriding the Equals () method in an object, when an object is stored in the HashSet collection, it is not possible to modify the field in the object that participates in the calculation of the hash value. Otherwise, the object's modified hash value differs from the hash value that was originally stored in the HashSet collection. At this point, if you want to delete an object that is stored in, you cannot delete the hash value correctly, which can cause a memory leak.
Package Com.internet;import java.util.*;p ublic class reflecttest4{public static void Main (String[]args) { //Programming for parent classes or interface-oriented programming Collection c1=new HashSet (); MyTest1 t1=new MyTest1 (2,2) ; MyTest1 t2=new MyTest1 (3,4) ; MyTest1 t3=new MyTest1 (2,2) ; C1.add (t1); C1.add (T2); C1.add (T3); C1.add (t1); System.out.println (C1.size ()); Results: 2 t1.x =4; C1.remove (t1); System.out.println (C1.size ()); Results: 2 } }
from the top we can see that the T1 object was not deleted correctly. Because the program modifies the value x that can generate hashcode in T1, the resulting hashcode is inconsistent with the stored in, and cannot be deleted.
Java Foundation Consolidation Series (V): Comparison of ArrayList and HashSet, and hashcode analysis