Examples of HashSet analysis and hashset Analysis

Source: Internet
Author: User

Examples of HashSet analysis and hashset Analysis

* 1. inherits from the abstract class AbstractSet to implement interfaces Set, Cloneable, and Serializable;
* 2. The elements are unordered;
* 3. elements cannot be repeated;
* 4. Use the hash algorithm to insert data, which is fast;
* 5. Non-thread security and lightweight;
* 6. The interface Cloneable can be copied;
* 7. The interface Serializable can be serialized to the disk;

 

1 package com. smbea. demo; 2 3 import java. util. hashSet; 4 import java. util. iterator; 5 import java. util. set; 6 7/** 8*1. inherited from the abstract class AbstractSet to implement interface Set, Cloneable, and Serializable; 9*2. No sequence of elements; 10*3. elements cannot be repeated; 11*4. Use a hash algorithm to insert data at a high speed. 12*5. Non-thread-safe and lightweight. 13*6. The interface Cloneable can be copied; 14*7. Because the interface Serializable is implemented, it can be serialized to the disk at 15 * @ author hapday16 * @ date, January 1, July 9, 2016: 1617*18 */19 public class HashSetDemo {20 public static void main (String [] args) {21 elementUnrepeatableTest (); 22 23 elementInconsecutiveTest (); 24} 25 26/** 27 * Verify [elements cannot be repeated] 28 */29 public static void elementUnrepeatableTest () {30 int num1 [] = new int [] {7, 8, 9}; 31 int num2 [] = num1; 32 33 Set <Object> set = new HashSet <Object> (); 34 set. add (1); 35 set. add (2); 36 set. add (3); 37 set. add (3); 38 set. add ("string"); 39 set. add (num1); 40 set. add (num2); 41 set. add ("Chinese"); 42 set. add ("Chinese cannot be repeated"); 43 set. add ("Chinese cannot be repeated"); 44 45 System. out. println ("*** elements cannot be repeated ***"); 46 Iterator <Object> iterator = set. iterator (); 47 while (iterator. hasNext () {48 System. out. print (iterator. next () + "-"); 49} 50 51 System. out. println ("\ n *** elements cannot be repeated (forEach) ***"); 52 for (Object object Object: set) {53 System. out. print (object + "-"); 54} 55 56 System. out. println ("\ n *** elements cannot be repeated (toString () ***"); 57 System. out. print (set); 58} 59 60/** 61 * [element unordered] 62 */63 public static void elementInconsecutiveTest () {64 Set <Integer> set = new HashSet <Integer> (); 65 for (int index = 1; index <= 100000; index ++) {66 set. add (index); // add 0.1 million element 67} 68 69 boolean exists = false; // whether the element exists: true-exist; false-70 int count = 0 does not exist; // Number of exist, multiple executions, we found that when the number of elements is 10 W, this value is unchanged, all are 17232, to some extent, the hash algorithm is relatively stable, 71 int current = 0; // The current element 72 int previous = 0; // The last element 73 Iterator <Integer> iterator = set. iterator (); 74 while (iterator. hasNext () {75 current = iterator. next (); 76 if (current <= previous) {77 exists = true; 78 count ++; 79} 80 81 previous = current; 82} 83 84 System. out. println ("\ n *** element unordered ***"); 85 System. out. print ("whether there is no order:" + exists + ", the possible order is:" + count); 86} 87}

 

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.