-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPowerSet.java
More file actions
42 lines (36 loc) Β· 1.19 KB
/
Copy pathPowerSet.java
File metadata and controls
42 lines (36 loc) Β· 1.19 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
package Chapter7.Day47;
import java.util.*;
public class PowerSet {
public static final <E> Collection<Set<E>> of(Set<E> s) {
List<E> src = new ArrayList<>(s);
if(src.size() > 30) {
throw new IllegalArgumentException("μ§ν©μ μμκ° λ무 λ§μ΅λλ€(μ΅λ 30κ°).: " + s);
}
return new AbstractList<Set<E>>() {
@Override
public int size() {
return 1 << src.size();
}
@Override
public boolean contains(Object o) {
return o instanceof Set && src.containsAll((Set) o);
}
@Override
public Set<E> get(int index) {
Set<E> result = new HashSet<>();
for (int i = 0; index != 0; i++, index >>=1) {
if((index & 1) == 1) {
result.add(src.get(i));
}
}
return result;
}
};
}
public static void main(String[] args) {
Collection<Set<String>> of = of(Set.of("a", "b", "c", "d", "e"));
for (Set<String> strings : of) {
System.out.println(strings);
}
}
}