JAVA learning lesson 36th (Common Object API)-Set framework (IV)-Set: HashSet Set demonstration

Source: Internet
Author: User

JAVA learning lesson 36th (Common Object API)-Set framework (IV)-Set: HashSet Set demonstration

With the development of Java, I feel like I have been using ACM for a year. It is wise to understand a lot of data structures and algorithms in Java.

The Set contains two sub-classes: HashSet Set and TreeSet.

The elements in the Set are not repeated and unordered.

1. HashSet set

API documentation explanation: This implementationSetInterface, which consists of a hash table (actuallyHashMapInstance. It does not guarantee the set iteration sequence; in particular, it does not ensure that the sequence remains unchanged. This class is allowedNullElement.

This provides stable performance for basic operations,Note that this implementation is not synchronous.

It can be concluded from the above: HashSet set method: the internal data structure is a hash table and is not synchronized. No matter how many duplicate elements are there, only one is saved.

import java.util.HashSet;import java.util.Iterator;public class Main {public static void main(String[] args) {HashSet hash = new HashSet();hash.add("123");hash.add("456");hash.add("asd");hash.add("789");Iterator it = hash.iterator();while(it.hasNext()){System.out.println(it.next());}}}

Hash table

The data is stored by the hash algorithm and is judged twice. The first time the data is located, the second time the data is located. For example, the "AB" is already stored, the location obtained by the "ba" algorithm is the location of "AB". In this case, the second judgment is required to determine whether the content is the same. If the content is different, a new location is obtained through the algorithm., ACM generally uses linear detection to further hash or link address.


Summary

Check whether the elements in the hash table are the same:

1. Check whether the hash values of the two elements are the same. If they are the same, check whether the content of the two objects is the same.

2. determine that the hash value is the same. The HashCode method (returns the hash code of this string) determines that the content is the same. The equals Method

Note: If the hash value is different, you do not need to determine whether the content is the same for the second time.


Hash Table demonstration

Import java. util. hashSet; import java. util. iterator; class Man extends Object // You must inherit the Object class to re-write HshCode, equals {private String name; private int age; public Man () {super (); // TODO Auto-generated constructor stub} public Man (String name, int age) {super (); this. name = name; // System. out. println ("1" + this); this. age = age ;}@ Overridepublic int hashCode () {final int prim = 31; // System. out. println ("namecode =" + na Me. hashCode (); return name. hashCode () + age * prim;} @ Overridepublic boolean equals (Object obj) {if (this = obj) return true; // System. out. println ("2" + this); if (! (Obj instanceof Man) throw new ClassCastException ("type error"); Man m = (Man) obj; return this. name. equals (m. name) & this. age = m. age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} public class Main {public static void main (String [] args) {HashSet hash = new HashSet (); hash. add (new Man ("a", 11); hash. add (new Man ("B", 12); hash. add (new Man ("c", 13); hash. add (new Man ("a", 11); Iterator it = hash. iterator (); while (it. hasNext () {Man M = (Man) it. next (); System. out. println (M. getName () + "..... "+ M. getAge ());}}}

Note: the first and second parts of the above Code represent different objects and cannot be confused.

Exercise

ArrayList demonstration

Import java. util. arrayList; import java. util. hashSet; import java. util. iterator; class Man extends Object {private String name; private int age; public Man () {super (); // TODO Auto-generated constructor stub} public Man (String name, int age) {super (); this. name = name; this. age = age ;}@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (! (Obj instanceof Man) throw new ClassCastException ("type error"); Man m = (Man) obj; return this. name. equals (m. name) & this. age = m. age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} public class Main {public static void main (String [] args) {ArrayList AL = new ArrayList (); AL. add (new Ma N ("a", 11); AL. add (new Man ("B", 12); AL. add (new Man ("c", 13); AL. add (new Man ("a", 11); printf (AL); AL = ArrayListHashDemo (AL); printf (AL); System. out. println (AL. remove (new Man ("a", 11); printf (AL);} public static ArrayList ArrayListHashDemo (ArrayList al) {ArrayList temp = new ArrayList (); iterator it = al. iterator (); while (it. hasNext () {Object obj = it. next (); if (! Temp. contains (obj) temp. add (obj);} return temp;} public static void printf (ArrayList al) {Iterator it = al. iterator (); while (it. hasNext () {Man man = (Man) it. next (); System. out. print (man. getName () + ".. "+ man. getAge () + ",");} System. out. printf ("\ n ");}}

API documentation explanation of contains: if the list contains the specified elements True. More specifically, if and only if the list contains (O = null? E = null: o. Equals(E ))Element EOnly return True.

Therefore, for the ArrayList set, the contains judgment is related to equals, while HashSet is related to HashCode and equals.

System. out. println (AL. remove (new Man ("a", 11); // Similarly, remove is related to equals and only determines the content.


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.