HashSet Collection and TreeSet collection under the Java Base Set set

Source: Internet
Author: User
Tags set set

Set of systems:

---------|CollectionRoot interface of a singleton collection
--------------|ListIf you implement the collection class for the list interface,Features: orderly, repeatable Note: ordering in a collection does not refer to the natural order, but the order in which it is added is consistent with the order in which it comes out.
------------------|ArrayListThe ArrayList bottom maintains an object array implementation. (thread is not secure),Features: Fast query speed, and delete the slow speed. ( orderly, repeatable )
------------------|LinkedListThe bottom of the LinkedList is implemented using a linked list structure,Features: Query speed is slow, adding and deleting faster. ( orderly, repeatable )
------------------|Vector (Learn about it)The underlying is also the implementation of maintaining an object array, the implementation is the same as ArrayList, but the vector is thread-safe and operating inefficient
--------------|SetIf you implement a collection class for the set interface,Features: Unordered, non-repeatable Note: unordered in a collection does not refer to the natural order, but the order in which the elements are added is inconsistent with the order of the elements.
------------------|HashSetThe implementation of the set interface features: There is no specific order, and no duplicate elements are allowed.
------------------| TreeSet The implementation of the set interface, features: no specific order, no duplicate elements are allowed.

1. HashSet Collection

The HashSet collection is an implementation of the set interface (set); The elements in the collection: unordered, and non-repeatable

1.1. Common methods of HashSet collection

Add (Object): adds the specified element to the collection
Remove (object): removes the specified element from the HashSet collection
size (): Returns the number of elements in the current collection
iterator (): An iterator that returns the elements in this collection
ToArray (): returns an array containing all the elements in this collection equivalent to converting a collection
contain (object): Determines whether an element is in the HashSet collection (contains a relationship)

In fact, these sets of common methods are almost the same, such as Some common methods of collection collection

1.2. HashSet collection cannot add repeating element principle

When the add element is called, the Hashcode method of the object is called first to determine if the hash code already exists, insert the element directly if it does not exist, and if it already exists, then call the Equals () method of the object to determine if it returns true. If true indicates that the element already exists, it can no longer be added to the HashSet collection, and if it returns false, the element does not exist, and the element may continue to be inserted into the HashSet collection.

1.3. Example
1 Package com.dhb.collection;2 3 import Java.util.HashSet;4 5 /**6 * @author dshore/2018-5-217  *8  */9 /*HashSet Implementation principle: When adding elements to the Hasset, the Hashcode method is called to get the Ahab value of the element, and then the element's hash value is shifted to calculate the storage location of the element stored in the Ahab Greek table. Ten * 1, if there is no element at the storage location where the hash code value of the element is calculated, then the element can be stored directly to that location One * 2, if there is already an element at the location where the hash code value of the element is stored, then the Equals method of the element is called to make a comparison with the element at that location, and if the Equals return value is true, then the element and the element that already exists in that position are treated as repeating elements. No further additions are allowed, and if equals returns False, the element can also be added to the collection.  A  * */ - classpersoner{ -     intID; the String name; -      -      PublicPersoner (intid,string name) { -          This. id=ID; +          This. name=name; -     } + @Override A      PublicString toString () {//overriding the function of ToString (): The return value is output as a string, and if not overridden, the return value is the memory address at         return "Person [id="+ ID +", Name="+ name +"]"; -     } - @Override -      Public intHashcode () {//overriding the role of Hashcode (): Judging by ID only -System. out. println ("========hashcode========");//Output 5 times cause: As long as add () How many times, hashcode () run how many times.  -         returnID; in     } - @Override to      PublicBoolean equals (Object obj) {//overriding the role of Equals (): Use ID to determine if it is the same element +System. out. println ("====== This is the Equals method ========");//Output The reason: only the element to be judged, equals () runs. such as: Only the dog's place, only need to judge (because only with the ID) -Personer p=(personer) obj; the         return  This. id==p.id; *     } $ }Panax Notoginseng  Public classDemo11 { -      the      Public Static voidMain (string[] args) { +Hashset<personer>Set=NewHashset<personer> ();//<personer>: Represents generics A         Set. Add (NewPersoner ( the,"Dog Doll")); the         Set. Add (NewPersoner ( -,"Dog left")); +         Set. Add (NewPersoner (119,"Dog Eggs")); -         Set. Add (NewPersoner ( +,"Dog Eggs")); $          $         //Assuming the numbers match, it's the same person. -System. out. println ("has the element been added successfully ?"+Set. Add (NewPersoner ( the,"Dog Doll")));//return Value: False if the Equals () method is not overridden, then the return value is true. Cause at the top of the code -System. out. println (Set);//return value: [Person [id=119, name= Dog egg], person [id=110, name= dog], person [id=10000, name= Dog egg], person [id=120, Name= Dog left]] the     } -}

Example 2

1 Package com.dhb.collection;2 3 /**4 * @author dshore/2018-5-215  *6  */7  Public classDemo13 {8      Public Static voidMain (string[] args) {9String str1="Hello";TenString str2=NewString ("Hello"); OneSystem. out. println ("two of the same object?"+ (STR1==STR2));//return value: false ASystem. out. println ("Str1 's Hascode:"+str1.hashcode ());//return value: 99162322 -System. out. println ("STR2 's Hascode:"+str2.hashcode ());//return value: 99162322 -         /* the * Hashcode the memory address represented by default, the string class has rewritten the Hascode method of object - * Note: If the contents of the two strings are consistent, the returned Hashcode code will be the same -          * */ -     } +}
2. TreeSet Collection

The TreeSet collection is also an implementation of the set interface (set); The elements in the collection: unordered, and non-repeatable

2.1.

Original Dshore

Author's homepage:http://www.cnblogs.com/dshore123/

Source:https://www.cnblogs.com/dshore123/p/9068474.html

Welcome reprint, reprint must explain the source. ( If this article is helpful to you, you can click on the lower right corner of the recommendation , or comments, thank you!) )

HashSet Collection and TreeSet collection under the Java Base Set set

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.