public static void main(String[] args) { SortedSet<String> sortedSet = new TreeSet<String>(); Collections.addAll(sortedSet, "one two three four five six seven eight".split(" ")); print(sortedSet); String low = sortedSet.first(); String high = sortedSet.last(); print(low); print(high); Iterator<String> it = sortedSet.iterator(); for (int i = 0; i <= 6; i++) { if (i == 3) low = it.next(); if (i == 6) high = it.next(); else it.next(); } print(low); print(high); print(sortedSet.subSet(low, high)); print(sortedSet.headSet(high)); print(sortedSet.tailSet(low)); }
public static void main(String[] args) { print(list); print( "'list' disjoint (Four)?: " + Collections.disjoint(list, Collections.singletonList("Four"))); print("max: " + Collections.max(list)); print("min: " + Collections.min(list)); print("max w/ comparator: " + Collections.max(list, String.CASE_INSENSITIVE_ORDER)); print("min w/ comparator: " + Collections.min(list, String.CASE_INSENSITIVE_ORDER)); List<String> sublist = Arrays.asList("Four five six".split(" ")); print("indexOfSubList: " + Collections.indexOfSubList(list, sublist)); print("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); Collections.replaceAll(list, "one", "Yo"); print("replaceAll: " + list); Collections.reverse(list); print("reverse: " + list); Collections.rotate(list, 3); print("rotate: " + list); List<String> source = Arrays.asList("in the matrix".split(" ")); Collections.copy(list, source); print("copy: " + list); Collections.swap(list, 0, list.size() - 1); print("swap: " + list); Collections.shuffle(list, new Random(47)); print("shuffled: " + list); Collections.fill(list, "pop"); print("fill: " + list); print("frequency of 'pop': " + Collections.frequency(list, "pop")); List<String> dups = Collections.nCopies(3, "snap"); print("dups: " + dups); print("'list' disjoint 'dups'?: " + Collections.disjoint(list, dups)); // Getting an old-style Enumeration: Enumeration<String> e = Collections.enumeration(dups); Vector<String> v = new Vector<String>(); while (e.hasMoreElements()) v.addElement(e.nextElement()); // Converting an old-style Vector // to a List via an Enumeration: ArrayList<String> arrayList = Collections.list(v.elements()); print("arrayList: " + arrayList); }