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]