[Java Basics] usage differences of hashset, javashashset, and treeset

Source: Internet
Author: User

Hashset: Hash Tables store information by means of a mechanism called hash, and elements are not stored in a specific order;

Linkedhashset: Maintain the chain table of the Set in the order of element insertion, and allow iteration in the set in the order of insertion;

Treeset: Provides an implementation that uses the tree structure to store the set interface. objects are stored in ascending order, and the access and Traversal Time is fast.

Use Case code:

Package com. test;

Import java. util. hashset;
Import java. util. linkedhashset;
Import java. util. treeset;

/**
* @ Description comparison of several sets
* Hashset: Hash Tables store information by using a mechanism called the hash method, and elements are not stored in a specific order;
* Linkedhashset: maintains the chain table of the Set in the order of element insertion, and allows iteration in the set in the insert order;
* Treeset: provides an implementation that uses the tree structure to store the set interface. objects are stored in ascending order, and access and traversal take a short time.
* @ Author Zhou-jingxian
*
*/
Public class setdemo {

Public static void main (string [] ARGs ){

Hashset <string> HS = new hashset <string> ();
HS. Add ("B ");
HS. Add ("");
HS. Add ("D ");
HS. Add ("e ");
HS. Add ("C ");
HS. Add ("F ");
System. Out. println ("hashset sequence: \ n" + HS );

Linkedhashset <string> LHS = new linkedhashset <string> ();
LHS. Add ("B ");
LHS. Add ("");
LHS. Add ("D ");
LHS. Add ("e ");
LHS. Add ("C ");
LHS. Add ("F ");
System. Out. println ("sequence hashset sequence: \ n" + LHS );

Treeset <string> TS = new treeset <string> ();
TS. Add ("B ");
TS. Add ("");
TS. Add ("D ");
TS. Add ("e ");
TS. Add ("C ");
TS. Add ("F ");
System. Out. println ("treeset sequence: \ n" + TS );
}
}

Output result:

Hashset sequence:
[D, E, F, A, B, C]
Sequence hashset:
[B, A, D, E, C, F]
Treeset sequence:
[A, B, c, d, e, f]

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.