Show Me code! In the difference code,
Public Static voidTesthashset () {HashSet<String> set =NewHashset<>(); Set.add ("Zhang Fei"); Set.add ("Zhang Liao"); Set.add ("Zhang Jean"); Set.add ("Zhang Jean"); Set.add ("1"); Set.add ("3"); Set.add ("2"); System.out.println ("--hashset--"); for(String string:set) {System.out.println (string); } } Public Static voidTesttreeset () {TreeSet<String> set =NewTreeset<>(); Set.add ("Zhang Fei"); Set.add ("Zhang Liao"); Set.add ("Zhang Jean"); Set.add ("Zhang Jean"); Set.add ("1"); Set.add ("3"); Set.add ("2"); Iterator Iterator=Set.iterator (); System.out.println ("--treeset--"); while(Iterator.hasnext ()) {System.out.println (Iterator.next ());; } } Public Static voidTestlinkhashset () {Linkedhashset<String> set =NewLinkedhashset<>(); Set.add ("Zhang Fei"); Set.add ("Zhang Liao"); Set.add ("Zhang Jean"); Set.add ("Zhang Jean"); Set.add ("1"); Set.add ("3"); Set.add ("2"); System.out.println ("--linkhashset--"); for(String string:set) {System.out.println (string); } } Public Static voidMain (string[] args) {//Unified features non-thread-safe and not repeatableMyset.testhashset ();/** (Zhang Liao, 1, Zhang Fei, 2, 3, Zhang Jan)Features: unordered, non-repeating, data structure: Hash list HashMap parent class Abstractset*/Myset.testlinkhashset ();/** (Zhang Fei, Zhang Liao, Zhang Jan, 1,3,2)Features: According to the order of insertion, not repeating, data structure: bidirectional loop linked list, implement using Linkedhashmap parent class HashSet*/Myset.testtreeset (); /** (Zhang Jan, Zhang Liao, Zhang Fei)Features: Automatic sorting, non-repetition, data structure: red-black tree implementation of parent class based on TreeMap Abstractset*/ }
Set of the Java Collection framework