Hashset, treeset, and enumset of the set Interface

Source: Internet
Author: User

Set interface: It is basically the same as collection. It does not provide any additional methods. In fact, set is a collection, but its behavior is different (set cannot contain repeated elements ).

I. hashset

Hashset is a typical implementation of the set interface. hashset stores elements based on the hash algorithm, so it has good access and search performance. It has the following features:

1. The order of elements cannot be ensured, and the order may change.

2. hashset is asynchronous.

3. The set element value can be null.

4. When an element is saved to a hashset set, hashset calls the hashcode () method of the object to obtain the hashcode value of the object, then, the location of the object stored in the hashset is determined based on the hashcode value.

Hashset also has a subclass of linkedhashset, which is used to determine the storage location of elements based on the hashcode value of the element. However, it also uses a linked list to maintain the order of elements, in this way, the elements seem to be saved in the order of insertion. That is to say, when traversing the elements in the linkedhashset set, it will access the elements in the set in the order of element addition. Therefore, the performance of linkedhashset is slightly lower than that of hashset, but it will have good performance when accessing all elements iteratively, because it maintains the internal order by using a linked list.

 

 

 

Ii. treeset 

Treeset is the only implementation of the sortset interface. treeset can ensure that the collection elements are in the sorting state. Compared with hashset, treeset provides several additional methods:

1. comparator (): return the comparator used by the current set, or return NULL, indicating that the comparator is sorted in a natural way.

2. Object first (): returns the first element in the set.

3. Object last (): returns the last element in the set.

4. Object lower (Object E): returns an element located before the specified Element in the collection.

5. Object higher (Object E): returns an element located after the specified Element in the collection.

6. sortedset subset (from element,
Element): returns the child set of this set, ranging from element to element (closure ).

7. sortedset headset (toelement): returns a subset of this set, which consists of elements smaller than toelement.

8. sortedset tailset (fromelement): returns a subset of the Set, which consists of elements greater than or equal to fromelement.

 

Public static void main (string [] ARGs ){
Treeset num = new treeset ();
Num. Add (5 );
Num. Add (2 );

Num. Add (10 );
Num. Add (-9 );
System. Out. println (Num); // output: [-9, 2, 5, 10] The elements of the set are already sorted.
System. Out. println (Num. First (); // output-9
System. Out. println (Num. Last (); // output 10 );
System. Out. println (Num. Headset (4); // output-9, 2 returns a subset smaller than 4
System. Out. println (Num. tailset (5); // output returns a subset greater than or equal to 5
System. Out. println (Num. subset (-9,5); // output-9,2 returns a subset greater than or equal to-9 and less than 5
}

As shown in the preceding procedure, treeset is not sorted by element insertion order, but by element value. Treeset supports two sorting methods: Natural sorting and custom sorting.

1. Natural sorting: treeset calls the compareto (Object
(OBJ) method to calculate the size relationship between elements, and then sort the set elements in ascending order. If you try to add an object to the treeset, the class of the object must implement the comparable interface. Otherwise, the program will encounter a classcastexception.

2. Custom sorting: If you need to implement custom sorting, you can use the help of the compa interface, which contains an int compare (O1, O2) method, this method is used to calculate the size of two numbers. If a positive integer is returned, it indicates O1> O2; if 0 is returned, it indicates O1 = O2; if a negative integer is returned, it indicates O1 <02.

Note: you cannot add objects of different types to treeset. Otherwise, classcastexception may occur.

 

 

 

Iii. enumset 

1. All values in enumset must be values of the specified enumeration type. The elements of enumset are also ordered. The order of elements in the set is determined based on the defined order of enumeration values.

2. The enumset set does not allow null elements. Otherwise, an nullpointerexception exception is thrown.

3. The enumset class does not expose any constructor to create instances of this class. The program should use the static method provided by it to create the enumset object. The common static method is as follows:

Static enumset allof (class elementtype): creates an enumset that contains all enumeration values in the specified enumeration class.
Static enumset complementof (enumset S): creates an enumset with the same element type as the specified enumset, the new enumset set contains the enumerated values that are not included in the original enumset set and the rest of this enumeration class.
Static enumset copyof (collection C): creates an enumset set using a common set.
Static enumset copyof (enumset E): creates an enumset that has the same element type and the same set element as the specified enumset.
Static enumset noneof (class elementtype): creates an empty enumset whose element type is the specified enumeration type.
Static enumset of (e first, e... rest): creates an enumset that contains one or more enumeration values. The incoming enumerated values must belong to the same enumeration class.
Static enumset range (E from, e to): creates an enumset that contains all enumeration values from the from enumeration value to the to enumeration value range.

 

Public static void main (string [] ARGs ){
Enumset e1 = enumset. Allof (season1.class );
// Create an enumset. The set element is all the enumerated values in season.
System. Out. println (E1); // output [spring,
Summer, fall, winter]
Enumset e2 = enumset. noneof (season1.class); // create an enumset empty set and specify that its set element is the enumerated value of season1.
E2.add (season1.winter );
E2.add (season1.spring );
System. Out. println (E2); // output [Spring, Winter]
Enumset E3 = enumset. Of (season1.summer, season1.winter); // create an enumset set with the specified enumerated Value
System. Out. println (E3); // output [Summer,
Winter]
Enumset E4 = enumset. Range (season1.summer, season1.winter );
System. Out. println (E4); // output [Summer,
Fall, winter]
Enumset E5 = enumset. complementof (E4 );
System. Out. println (E5); // output [Spring]
}

 

 

Summary:

1. The performance of hashset is better than that of treeset, because treeset requires an additional red/black tree algorithm to maintain the order of the Set elements. treeset is used only when a set that maintains the sorting is required.

2. enumset provides the best performance, but it can only store enumeration values.

3. They are all thread-insecure.

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.