|
| 1 | +// collectiontopics/CollectionMethods.java |
| 2 | +// (c)2017 MindView LLC: see Copyright.txt |
| 3 | +// We make no guarantees that this code is fit for any purpose. |
| 4 | +// Visit http://OnJava8.com for more book information. |
| 5 | +// Things you can do with all Collections |
| 6 | +import java.util.*; |
| 7 | +import static onjava.HTMLColors.*; |
| 8 | + |
| 9 | +public class CollectionMethods { |
| 10 | + public static void main(String[] args) { |
| 11 | + Collection<String> c = |
| 12 | + new ArrayList<>(LIST.subList(0, 4)); |
| 13 | + c.add("ten"); |
| 14 | + c.add("eleven"); |
| 15 | + show(c); |
| 16 | + border(); |
| 17 | + // Make an array from the List: |
| 18 | + Object[] array = c.toArray(); |
| 19 | + // Make a String array from the List: |
| 20 | + String[] str = c.toArray(new String[0]); |
| 21 | + // Find max and min elements; this means |
| 22 | + // different things depending on the way |
| 23 | + // the Comparable interface is implemented: |
| 24 | + System.out.println( |
| 25 | + "Collections.max(c) = " + Collections.max(c)); |
| 26 | + System.out.println( |
| 27 | + "Collections.min(c) = " + Collections.min(c)); |
| 28 | + border(); |
| 29 | + // Add a Collection to another Collection |
| 30 | + Collection<String> c2 = |
| 31 | + new ArrayList<>(LIST.subList(10, 14)); |
| 32 | + c.addAll(c2); |
| 33 | + show(c); |
| 34 | + border(); |
| 35 | + c.remove(LIST.get(0)); |
| 36 | + show(c); |
| 37 | + border(); |
| 38 | + // Remove all components that are |
| 39 | + // in the argument collection: |
| 40 | + c.removeAll(c2); |
| 41 | + show(c); |
| 42 | + border(); |
| 43 | + c.addAll(c2); |
| 44 | + show(c); |
| 45 | + border(); |
| 46 | + // Is an element in this Collection? |
| 47 | + String val = LIST.get(3); |
| 48 | + System.out.println( |
| 49 | + "c.contains(" + val + ") = " + c.contains(val)); |
| 50 | + // Is a Collection in this Collection? |
| 51 | + System.out.println( |
| 52 | + "c.containsAll(c2) = " + c.containsAll(c2)); |
| 53 | + Collection<String> c3 = |
| 54 | + ((List<String>)c).subList(3, 5); |
| 55 | + // Keep all the elements that are in both |
| 56 | + // c2 and c3 (an intersection of sets): |
| 57 | + c2.retainAll(c3); |
| 58 | + show(c2); |
| 59 | + // Throw away all the elements |
| 60 | + // in c2 that also appear in c3: |
| 61 | + c2.removeAll(c3); |
| 62 | + System.out.println( |
| 63 | + "c2.isEmpty() = " + c2.isEmpty()); |
| 64 | + border(); |
| 65 | + // Functional operation: |
| 66 | + c = new ArrayList<>(LIST); |
| 67 | + c.removeIf(s -> !s.startsWith("P")); |
| 68 | + c.removeIf(s -> s.startsWith("Pale")); |
| 69 | + // Stream operation: |
| 70 | + c.stream().forEach(System.out::println); |
| 71 | + c.clear(); // Remove all elements |
| 72 | + System.out.println("after c.clear():" + c); |
| 73 | + } |
| 74 | +} |
| 75 | +/* Output: |
| 76 | +AliceBlue |
| 77 | +AntiqueWhite |
| 78 | +Aquamarine |
| 79 | +Azure |
| 80 | +ten |
| 81 | +eleven |
| 82 | +****************************** |
| 83 | +Collections.max(c) = ten |
| 84 | +Collections.min(c) = AliceBlue |
| 85 | +****************************** |
| 86 | +AliceBlue |
| 87 | +AntiqueWhite |
| 88 | +Aquamarine |
| 89 | +Azure |
| 90 | +ten |
| 91 | +eleven |
| 92 | +Brown |
| 93 | +BurlyWood |
| 94 | +CadetBlue |
| 95 | +Chartreuse |
| 96 | +****************************** |
| 97 | +AntiqueWhite |
| 98 | +Aquamarine |
| 99 | +Azure |
| 100 | +ten |
| 101 | +eleven |
| 102 | +Brown |
| 103 | +BurlyWood |
| 104 | +CadetBlue |
| 105 | +Chartreuse |
| 106 | +****************************** |
| 107 | +AntiqueWhite |
| 108 | +Aquamarine |
| 109 | +Azure |
| 110 | +ten |
| 111 | +eleven |
| 112 | +****************************** |
| 113 | +AntiqueWhite |
| 114 | +Aquamarine |
| 115 | +Azure |
| 116 | +ten |
| 117 | +eleven |
| 118 | +Brown |
| 119 | +BurlyWood |
| 120 | +CadetBlue |
| 121 | +Chartreuse |
| 122 | +****************************** |
| 123 | +c.contains(Azure) = true |
| 124 | +c.containsAll(c2) = true |
| 125 | +c2.isEmpty() = true |
| 126 | +****************************** |
| 127 | +PapayaWhip |
| 128 | +PeachPuff |
| 129 | +Peru |
| 130 | +Pink |
| 131 | +Plum |
| 132 | +PowderBlue |
| 133 | +Purple |
| 134 | +after c.clear():[] |
| 135 | +*/ |
0 commit comments