HashSet, edged, we are most used to. The other two, we are less used to, today, we look at their differences.
ImportJava.util.HashSet;ImportJava.util.Set; Public classHashsettester { Public Static voidMain (string[] args) {Set<Integer> set =NewHashset<integer>(); Set.add (15); Set.add (31); Set.add (12); Set.add (123); Set.add (42); for(Integer i:set) {System.out.println (i); } }}View Code
HashSet, the ability to quickly find key values.
See its realization, depends on the implementation of HashMap . Print the log:
42123123115
If modified to use TreeSet, this is the log.
TreeSet the underlying maintenance is a red-black tree , maintaining an orderly sequence , the element must implement the comparable interface.
12153142123
and using linkedtreeset, the log is like this.
Keep the insertion order and support Quick Lookup of key values.
Linkedtreeset is the inheritance TreeSet construction method, and TreeSet's construction method uses the linkedhashmap, the visible principle hides in the linkedhashmap inside.
15311212342
Common set types for "Java", HashSet, TreeSet, Linkedhashset