// understandingcollections/SetOrder.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. import java.util.*; import onjava.HTMLColors; public class SetOrder { static String[] sets = { "java.util.HashSet", "java.util.TreeSet", "java.util.concurrent.ConcurrentSkipListSet", "java.util.LinkedHashSet", "java.util.concurrent.CopyOnWriteArraySet", }; static List RLIST = new ArrayList<>(HTMLColors.LIST); static { Collections.reverse(RLIST); } public static void main(String[] args) throws Exception { for(String type: sets) { System.out.format("[-> %s <-]\n", type.substring(type.lastIndexOf('.') + 1)); @SuppressWarnings("unchecked") Set set = (Set) Class.forName(type).newInstance(); set.addAll(RLIST); set.stream() .limit(10) .forEach(System.out::println); } } } /* Output: [-> HashSet <-] MediumOrchid PaleGoldenRod Sienna LightSlateGray DarkSeaGreen Black Gainsboro Orange LightCoral DodgerBlue [-> TreeSet <-] AliceBlue AntiqueWhite Aquamarine Azure Beige Bisque Black BlanchedAlmond Blue BlueViolet [-> ConcurrentSkipListSet <-] AliceBlue AntiqueWhite Aquamarine Azure Beige Bisque Black BlanchedAlmond Blue BlueViolet [-> LinkedHashSet <-] YellowGreen Yellow WhiteSmoke White Wheat Violet Turquoise Tomato Thistle Teal [-> CopyOnWriteArraySet <-] YellowGreen Yellow WhiteSmoke White Wheat Violet Turquoise Tomato Thistle Teal */