Java Collection Framework rule set--set

Source: Internet
Author: User

Collection:
Java supports three main types:
1. Rule set (SET)
Used to store a set of non-repeating elements
2. Linear table (list)
Used to store an ordered collection of elements
3. Queuing (queue)
A queue in the same data structure that stores the object in FIFO mode
Note: All interfaces and classes in the Java collection framework are stored in the Java.util package

Set
Set includes:
1. Hash set HashSet
2. Chain Hash Set Linkedhashset
3. Tree-Type set TreeSet
  

HashSet:
1.HashSet belongs to the rule set, so it does not allow duplicate elements
2. The elements of a hash set do not have a specific order

1 ImportJava.util.*;2  Public classHashset_test {3      Public Static voidMain (string[] args) {4set<string> S1 =NewHashSet ();5S1.add ("Apple");6S1.add ("Huawei");7S1.add ("Samsung");8S1.add ("Apple");9 System.out.println (S1);TenHASHSET&LT;STRING&GT;S2 =NewHashSet (); OneS2.add ("Apple"); AS2.add ("Huawei"); -S2.add ("Samsung"); -S2.add ("Apple"); the System.out.println (S2); -     } -}
View Code
1 [Apple, Samsung, Huawei] 2 [Apple, Samsung, Huawei]
View Code

Linkedhashset:
It implements the extended HashSet class in the form of a linked list
1. It supports ordering elements within a rule set
2. It can be extracted in the order that the elements are inserted in the rule set
3. Belongs to the rule set, which does not allow duplicate elements

1  PackageOne ;2 ImportJava.util.LinkedHashSet;3 ImportJava.util.Set;4  Public classLinkedhashset_test {5      Public Static voidMain (string[] args) {6set<string> S1 =NewLinkedhashset ();7S1.add ("Apple");8S1.add ("Samsung");9S1.add ("Huawei");TenS1.add ("Apple"); One System.out.println (S1); A     } -}
View Code
1 [Apple, Samsung, Huawei]
View Code

TreeSet:
1. Provide more methods
2. You can ensure that the elements in the rule set are ordered
3. Belongs to the rule set, which does not allow duplicate elements

1  PackageOne ;2 ImportJava.util.Set;3 ImportJava.util.TreeSet;4  Public classTreeset_test {5      Public Static voidMain (string[] args) {6SET&LT;STRING&GT;S1 =NewTreeSet ();7S1.add ("Apple");8S1.add ("Samsung");9S1.add ("Huawei");TenS1.add ("Apple"); One System.out.println (S1); A     } -}
View Code
1 [Apple, Huawei, Samsung]
View Code

Summarize:

HashSet Sort by hash function

Linkedhashset Sort by Insertion Order

TreeSet in alphabetical order


HashSet: A hash table stores information by using a mechanism called hashing, and the elements are not stored in a particular order;
Linkedhashset: maintains a linked table of the collection in the order in which the elements are inserted, allowing iterations in the collection in the order in which they are inserted;
TreeSet: Provides an implementation that uses a tree structure to store a set interface, where objects are stored in ascending order, and the time to access and traverse is fast.

Java Collection Framework rule 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.