import java.util.List; import java.util.HashSet; import java.util.Collections; import java.util.TreeSet; import java.util.Set; /** An example of the factory pattern for CSE 331 sections. */ public class SetFactory { // this class cannot be instantiated private SetFactory() { throw new UnsupportedOperationException(); } /** Returns a Set containing the unique elements in the input list. * If the input list is ordered, the Set maintains an ordering * property. * * @param list A list of objects. Can contain duplicates. Elements must be * comparable. * * @param T the type of the elements in the list. * * @return a set containing the unique objects in the input. The set is * ordered iff the input list was sorted. */ public static Set produceSet(List list) { boolean sorted = true; for(int i = 1; i < list.size(); i++) { if(list.get(i).compareTo(list.get(i - 1)) < 0 ) { sorted = false; } } if (sorted) { return new TreeSet(list); } else { return new HashSet(list); } } }