Comparison and hashcode analysis of ArrayList and HashSet

Source: Internet
Author: User

comparison between ArrayList and HashSet

ArrayList is an ordered set of

HashSet by comparing two objects for equality, the same does not join the HashSet collection and does not allow duplication. How to compare? The Equals () method and the Hashcode () method must be overridden in the HashSet collection Object

Hashcode Analysis

objective to improve the efficiency of finding elements in the hash set, which divides the set into several storage areas, and each object can calculate a hash code. Hash codes are grouped, each group corresponds to a storage area, and the hash code of an object determines which area the object should be stored in.

HashSet is a collection of Access objects using a hashing algorithm, in which the hash code is grouped and the storage area of the object is divided by the method of the number n redundancy.

The object class defines a hashcode () method to return the hash code for each Java object, and when looking for an object from the HashSet collection, the Java system first calls the object's Hashcode () method to get the hash code of the object. The corresponding storage area is then found based on the hash code, and finally

Each element of the storage area is taken out of the Equals () method comparison with the object. This allows you to get a conclusion without traversing all the elements in the collection.

The HashSet collection has good object retrieval performance, but the HashSet collection stores objects with less efficiency.

public static void Main (string[] args) {//TODO auto-generated method stubcollection Collections = new HashSet (); ReflectTest2 r1 = new ReflectTest2 (3, 3); ReflectTest2 r2 = new ReflectTest2 (5, 5); REFLECTTEST2 R3 = new ReflectTest2 (3, 3); Collections.add (R1); Collections.add (R2); Collections.add (R3); Collections.add (R1);//!!! If the object is already stored in the HashSet collection, do not modify the value of the property that the object participates in the hashcode evaluation//If you get rid of the value of R1 property y, the HashSet function will not execute the object method r1.y = 2;collections.remove (r1); /If you let R1,R3 belong to the same object, you must override the Equals () method and the Hashcode () method in ReflectTest2//Hashcode () method to make the hash code of R1,R3 equal//If not HashSet collection. There is no need//The result of overriding the Equals () method and the Hashcode () method in the ReflectTest2 class is 2. Otherwise 3 System.out.println (Collections.size ());} 
public class ReflectTest2 {private int x;public int y;//override Equals () and Hashcode () method @overridepublic int Hashcode () {final int P Rime = 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; ReflectTest2 other = (ReflectTest2) obj;if (x! = other.x) return false;if (Y! = other.y) return False;return true;}}

PS: If the object is already stored in the HashSet collection, do not modify the value of the property that the object participates in the hashcode calculation. After the change, the object will produce a new hash code, Collections.remove (R1) In the example, and the statement will fail to execute successfully, causing a memory leak!!!!


Comparison and hashcode analysis of ArrayList and HashSet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.