Set of the Java Collection framework

Source: Internet
Author: User

Set Set

Elements cannot be duplicated, they are unordered.

Method is the same as list

HashSet: No guaranteed set Iteration order, no synchronization, internal data structure is a hash table

If you save a custom object, you need to override the Equals and Hashcode methods

Compare Hashcode First, if the hashcode is the same again equals method comparison, if still the same as the judgment element is no longer into the container, that is, the Add () method does not add elements

Linkedhashset: both orderly and unique

TreeSet: The elements in the set collection can be sorted and are not synchronized

The way to judge whether an element is unique is by comparing the method comparable returns 0, and returning 0 is equal

TreeSet How to sort the elements one:

Let the element (deposited object) implement the comparable interface and overwrite the comparable method

Determine size and sort by return value (0, positive, negative)

If a negative number is returned, indicating that the element to be added is greater than the element being compared, place the element you want to add to the right of the binary tree

Instead...

Way two:

If the object does not have a natural sort, you can make the collection itself comparable.

The TreeSet constructor is injected into a class object that implements the comparator interface and overrides the Compare method to sort;

(Note: The second method is used when both methods are in use)

Set of the Java Collection framework

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.