A big wave of Java Attack (vii) set interface for--java set

Source: Internet
Author: User
Tags set set

This paper mainly deals with the set interface and its implementation class of collection interface.

Set set, features: unordered, non-repeatable


As we have described above, the set set is similar to a jar, and the elements thrown in are not in the obvious order. If you want to think about it, it shouldn't be stored randomly. There must be a certain pattern of storage, and listen to my slow way.

the implementation class for the set interface:

  1. hashset class
    1. literally guessing, About the hash algorithm
    2. subclass linkedhashset, on the basis of which linked links were added, may be worn by the pointer
  2. sortedset interface
    1. literally guessing, already sorted, Just don't know how to sort the
    2. treeset class, The default is stored according to the tree structure?
  3. enumset class


Let's take the question and find the answer!

First, HashSet

1. Features: According to the hash algorithm to store the elements of the collection, has a good access and lookup properties.

2. How do I judge repetition? Determine where to store?

If you add a new element, first determine equals

if false , the add succeeds.

If you return true , then according to Hash algorithm judgment hashcode ()

If the same, the add fails.

If it is not the same, the add succeeds. Storage location: Determined based on The value of hashcode

3. Subclass Linkedhashset, features: Maintain the insertion order of elements at the same time, performance is lower than HashSet

Second, TreeSet, implement SortedSet interface, order (sort by element actual value, not insert order)

1. Contrast

HashSet class, using hash algorithm to determine the storage location

treeset sorting elements using red-black tree data structures

2.2 Methods of sorting

Natural Sort (default): Call the CompareTo (OJEBCT O) method of the comparable interface, in ascending order, to determine the storage location according to the red-black tree algorithm

Custom Ordering: Calling the Comparator interface int compare (t O1, T O2) method

Enumset, to determine the order of the collection elements in the order in which the enumeration values are defined within the enum class

1. Storage mode: Bit vector. Compact structure, high efficiency, low memory footprint.


Four, performance comparison

1. HashSet performance is always better than treeset, especially for common add and find operations.

Therefore, additional red-black tree algorithms are required in TreeSet to maintain the order of the collection elements.

Use TreeSet only if you need a set that remains sorted. Otherwise, it's all in HashSet.

2.LinkedHashSet is a subclass of HashSet because it has a linked list, so it is faster to traverse. Because of the additional cost of maintaining the linked list, the operation performance is slightly slower for normal insertions and deletions.

3.EnumSet is the best performance in all set implementations, but only the enumeration values of the same American drama class can be saved as a collection element.

Note: The three implementation classes of set: HashSet, TreeSet, and enumset are thread insecure and require manual synchronization of set sets. You typically use the Synchronizedsortedset method of the Collections tool class to "wrap" the set collection. This is best done at creation time, preventing accidental non-synchronous access to the set collection.

SortedSet s = collections.synchronizedsortedset (new TreeSet (...));


A big wave of Java Attack (vii) set interface for--java set

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.