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