-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample04a.java
More file actions
54 lines (41 loc) · 1.16 KB
/
Example04a.java
File metadata and controls
54 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.study.collection;
import java.util.*;
public class Example04a {
static void printCollection(String s, Collection<String> c) {
String[] a = c.toArray(new String[0]);
Arrays.sort(a);
System.out.printf("%s: %s\n", s, Arrays.toString(a));
}
public static void main(String[] args) {
Collection<String> c1 = new Stack<>();
Collection<String> c2 = new LinkedList<>();
Collection<String> c3 = new ArrayList<>();
for (int i = 0; i < 20; i += 2) {
String s = String.format("%02d", i);
c1.add(s);
}
printCollection("c1 (2의 배수)", c1);
for (int i = 0; i < 20; i += 3) {
String s = String.format("%02d", i);
c2.add(s);
}
printCollection("c2 (3의 배수)", c2);
c3.clear();
c3.addAll(c1);
c3.retainAll(c2);
printCollection("c1, c2 교집합", c3);
c3.clear();
c3.addAll(c1);
c3.removeAll(c2);
printCollection("c1, c2 차집합", c3);
c3.clear();
c3.addAll(c2);
c3.removeAll(c1);
printCollection("c2, c1 차집합", c3);
c3.clear();
c3.addAll(c1);
c3.removeAll(c2);
c3.addAll(c2);
printCollection("c1, c2 합집합", c3);
}
}