MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

collectiontopics/CollectionMethods.java:10–72  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

8
9public 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 // Discard all c2 elements that also appear in c3:
60 c2.removeAll(c3);
61 System.out.println(
62 "c2.isEmpty() = " + c2.isEmpty());
63 border();
64 // Functional operation:
65 c = new ArrayList<>(LIST);
66 c.removeIf(s -> !s.startsWith("P"));
67 c.removeIf(s -> s.startsWith("Pale"));

Callers

nothing calls this directly

Calls 10

borderMethod · 0.80
addAllMethod · 0.80
containsMethod · 0.80
isEmptyMethod · 0.80
showMethod · 0.65
maxMethod · 0.65
getMethod · 0.65
addMethod · 0.45
removeMethod · 0.45
streamMethod · 0.45

Tested by

no test coverage detected