"Java_ Set frame Set" HashSet, Linkedhashset, treeset use differences

Source: Internet
Author: User

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.

Sample code::

1  PackageCom.test2;2 3 ImportJava.util.HashSet;4 ImportJava.util.Iterator;5 ImportJava.util.LinkedHashSet;6 Importjava.util.List;7 ImportJava.util.Set;8 ImportJava.util.TreeSet;9 Ten  Public classset { One  A      Public Static voidLinkedhashset () { -         //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; -System.out.println ("Here is Linkedhashset:"); theSet<string> set =NewLinkedhashset<string>(); -Set.add ("AAA"); -Set.add ("D"); -Set.add ("Z"); +Set.add ("AAA"); -Set.add ("BBB"); +          for(Iterator<string> iter =set.iterator (); Iter.hasnext ();) { AString str =Iter.next (); at System.out.println (str); -         } -  -     } -  -      Public Static voidTreeSet () { in         //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.  -System.out.println ("Here is TreeSet:"); toSet<string> set =NewTreeset<string>(); +Set.add ("AAA"); -Set.add ("D"); theSet.add ("Z"); *Set.add ("AAA"); $Set.add ("BBB");Panax Notoginseng          for(Iterator<string> iter =set.iterator (); Iter.hasnext ();) { -String str =Iter.next (); the System.out.println (str); +         } A  the     } +  -      Public Static voidHashSet () { $         //A hash table stores information by using a mechanism called hashing, and elements are not stored in a particular order $System.out.println ("Here is HashSet:"); -Set<string> set =NewHashset<string>(); -Set.add ("AAA"); theSet.add ("D"); -Set.add ("Z");WuyiSet.add ("AAA"); theSet.add ("BBB"); -          for(Iterator<string> iter =set.iterator (); Iter.hasnext ();) { WuString str =Iter.next (); - System.out.println (str); About         } $  -     } -  -      Public Static voidMain (string[] args) { A Linkedhashset (); + hashset (); the TreeSet (); -  $     } the  the}

Result output:

Here is Linkedhashset:
Aaa
D
Z
Bbb
Here is HashSet:
Aaa
Bbb
D
Z
Here is TreeSet:
Aaa
Bbb
D
Z

"Java_ Set frame Set" HashSet, Linkedhashset, treeset use differences

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.