Java Learning Lesson 36th (Common Object API)-collection Framework (iv)-set collection: HashSet Collection Demo

Source: Internet
Author: User
Tags set set

With the deepening of Java learning, feel big for a year of ACM, it is smart, Java a lot of data structure, algorithm class things, it is much easier to understand

There are two major sub-categories under set set to develop common HashSet collections, TreeSet collections

The elements of the set set are non-repeating and unordered

First, HashSet collection

API documentation explains: This class implements the Set interface, which is supported by a hash table (actually a HashMap instance). It does not guarantee the set's iteration order, especially it does not guarantee that the order is constant. This class allows the use of null elements.

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

From the above can be summed up: HashSet Collection method: The internal data structure is a hash table, and is not synchronized, regardless of the number of duplicate elements, only one saved.

Import Java.util.hashset;import Java.util.iterator;public class Main {public static void main (string[] args) {HashSet have h = 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

data through the hash algorithm storage, will be judged by two times, the first to determine the location, the second will judge the content, such as "AB" has been stored, "BA" by the algorithm to get the position is "ab" position, then to pass the second judgment, determine whether the content is the same, not the same will be through the algorithm to get a new , the ACM typically undergoes a linear detection and re-hash or a chain address method.


Summarize

Hash table determines whether elements are the same:

1. Determine whether the hash value of the two elements is the same, and if the same, then determine whether the contents of the two objects are the same

2. Determine the same hash value, the Hashcode method (return the hash code of this string), the same content, the Equals method

Note: Unlike hash values, you do not need to determine the same content for the second time


Hash show shows

Import Java.util.hashset;import Java.util.iterator;class man extends object//must inherit the Object class to replicate 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 ("number 1th" +this); this.age = age;} @Overridepublic int hashcode () {final int prim = 31;//SYSTEM.OUT.PRINTLN ("Namecode =" +name.hashcode ()); return Name.hashcode () +age*prim;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;//system.out.println ("number 2nd" +this); obj instanceof man) throw new ClassCastException ("type error"); Mans 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.) {this.age = age;}} public class Main {public static void main (string[] args) {HashSet hash = new HashSet () Hash.add (New Man ("a", one)); Hash.add (New Man ("B", 12)); Hash.add (New Man ("C", "D")); Hash.add (New Man ("a", one)); Iterator it = Hash.iterator (); while (It.hasnext ()) {Mans M = (man) It.next (); System.out.println (M.getname () + "......" +m.getage ());}}

Special Note: The above code of 1th and 2nd number Two this, representing the different objects, can not be confused

Practice

A demonstration of ArrayList

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"); Mans 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.) {this.age = age;}} public class Main {public static void main (string[] args) {ArrayList AL = new ArrayList (); Al.add (New Man ("a", 11)); Al.add (New Man ("B", 12)); Al.add (New Man ("C", 13)); Al.add (New Man ("a", one));p rintf (AL); Al = Arraylisthashdemo (AL);p rintf (AL); System.out.println (Al.remove (New Man ("a", one)));p rintf (AL); public static ArrayList Arraylisthashdemo (ArrayliSt al) {ArrayList temp = new ArrayList (); Iterator it = Al.iterator (); while (It.hasnext ()) {Object obj = It.next (); if (!temp.c Ontains (obj)) temp.add (obj); return temp;} public static void printf (ArrayList al) {Iterator it = Al.iterator (), while (It.hasnext ()) {Mans man = (man) it.next (); System.out.print (Man.getname () + "..." +man.getage () + ",");} System.out.printf ("\ n");}}

API documentation for contains explanation: If the list contains the specified element, the returntrue。more precisely,When and only if the list contains content that satisfies the(O==null e==null:o. equals(e))The ElementseOnly return whentrue。

So for ArrayList sets, contains's judgment is related to equals, while HashSet is related to hashcode and equals

System.out.println (Al.remove ("a", 11));//Similarly, remove also has equals, only to judge the content


Java Learning Lesson 36th (Common Object API)-collection Framework (iv)-set collection: HashSet Collection Demo

Related Article

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.