| 9 | static List<String> list = Arrays.asList( |
| 10 | "one Two three Four five six one".split(" ")); |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println(list); |
| 13 | System.out.println("'list' disjoint (Four)?: " + |
| 14 | Collections.disjoint(list, |
| 15 | Collections.singletonList("Four"))); |
| 16 | System.out.println( |
| 17 | "max: " + Collections.max(list)); |
| 18 | System.out.println( |
| 19 | "min: " + Collections.min(list)); |
| 20 | System.out.println( |
| 21 | "max w/ comparator: " + Collections.max(list, |
| 22 | String.CASE_INSENSITIVE_ORDER)); |
| 23 | System.out.println( |
| 24 | "min w/ comparator: " + Collections.min(list, |
| 25 | String.CASE_INSENSITIVE_ORDER)); |
| 26 | List<String> sublist = |
| 27 | Arrays.asList("Four five six".split(" ")); |
| 28 | System.out.println("indexOfSubList: " + |
| 29 | Collections.indexOfSubList(list, sublist)); |
| 30 | System.out.println("lastIndexOfSubList: " + |
| 31 | Collections.lastIndexOfSubList(list, sublist)); |
| 32 | Collections.replaceAll(list, "one", "Yo"); |
| 33 | System.out.println("replaceAll: " + list); |
| 34 | Collections.reverse(list); |
| 35 | System.out.println("reverse: " + list); |
| 36 | Collections.rotate(list, 3); |
| 37 | System.out.println("rotate: " + list); |
| 38 | List<String> source = |
| 39 | Arrays.asList("in the matrix".split(" ")); |
| 40 | Collections.copy(list, source); |
| 41 | System.out.println("copy: " + list); |
| 42 | Collections.swap(list, 0, list.size() - 1); |
| 43 | System.out.println("swap: " + list); |
| 44 | Collections.shuffle(list, new Random(47)); |
| 45 | System.out.println("shuffled: " + list); |
| 46 | Collections.fill(list, "pop"); |
| 47 | System.out.println("fill: " + list); |
| 48 | System.out.println("frequency of 'pop': " + |
| 49 | Collections.frequency(list, "pop")); |
| 50 | List<String> dups = |
| 51 | Collections.nCopies(3, "snap"); |
| 52 | System.out.println("dups: " + dups); |
| 53 | System.out.println("'list' disjoint 'dups'?: " + |
| 54 | Collections.disjoint(list, dups)); |
| 55 | // Getting an old-style Enumeration: |
| 56 | Enumeration<String> e = |
| 57 | Collections.enumeration(dups); |
| 58 | Vector<String> v = new Vector<>(); |
| 59 | while(e.hasMoreElements()) |
| 60 | v.addElement(e.nextElement()); |
| 61 | // Converting an old-style Vector |
| 62 | // to a List via an Enumeration: |
| 63 | ArrayList<String> arrayList = |
| 64 | Collections.list(v.elements()); |
| 65 | System.out.println("arrayList: " + arrayList); |
| 66 | } |
| 67 | } |
| 68 | /* Output: |