Set collection in Java

Source: Internet
Author: User

import java.util.hashset;import java.util.iterator;import java.util.set;public class  Settest01 {/*set collection: Hashset1.hashset The bottom layer is actually a hashmap,hashmap with a hash table data structure. 2. The hash table is also called the hash table, the bottom of the hash table is an array, and each element in this array is a one-way list. Each unidirectional list has a unique hash value that represents the subscript of the array. The hash value on each node in a one-way list is equal. The hash value is actually a value that the key calls the Hashcode method, which is converted to "hash function". 3. How to add elements to the hash table: First call the Hashcode method of the stored key, after an algorithm to derive the hash value, if there is no such  hash value in this hash table, then add the element directly. If the hash value already exists, continue calling the Equals method between keys, and if the Equals method returns false, add the element. If the Equals method returns True, the addition of the element is discarded. 4.HashSet is actually the key part of the HashMap. What are the characteristics of HashSet, hashmap in the key  should have the same characteristics. 5.HashMap and HashSet initialization capacity are  16, the default load factor is 0.75*///ingress Public static void main (String[] args) {/ /create Set set Set s = new hashset ();//unordered S.add (1); S.add (1); S.add (+); S.add (+); S.add (88);// Traverse Iterator it = s.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} Conclusion: The elements stored in the HashSet collection or the key portion of the HashMap collection need to be rewritten simultaneously hashcode+equals          &nBsp;      set es = new hashset (); Employee e1 = new employee ("+", "JACK"); Employee e2 = new employee ("+", "JACK"); Employee e3 = new employee ("n", "SCOTT"); Employee e4 = new employee ("2001", "SUN"); Employee e5 = new employee ("n", "JIM"); Employee e6 = new employee ("3001", "COOK"); System.out.println (E1.hashcode ()); System.out.println (E2.hashcode ());//add Element Es.add (E1); Es.add (E2); Es.add (E3); Es.add (E4); Es.add (E5); Es.add (E6);// View the number of collection elements System.out.println (Es.size ());  //}}public class employee {private string  id;//idprivate string name;//namepublic string getno ()  {return id;} Public void setno (String id)  {this.id = id;} Public string getname ()  {return name;} Public void setname (String name)  {this.name&Nbsp;= name;} Construction method Public employee (String id, string name)  {super ();this.id = id; This.name = name;} Public employee () {}//overrides the Hashcode method. Public int hashcode ()  {return id.hashcode ();} Overridden the Equals method. Public boolean equals (object obj)  {if (obj==this) {return true;} Else{if (Obj instanceof employee) {employee e =  (Employee) obj;if (E.id.equals (this.id) &&e.name.equals (this.name)) {return true;}}} Return false;}


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1669561

Set collection in Java

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.