Skip to content

Commit 6d8ec4f

Browse files
javaeeeeematthewmorgan
authored andcommitted
custom-set: add to track (exercism#206)
* dibs: I will implement custom-set * Created an empty project * Added custom-set exercise to the Java track
1 parent 30abb92 commit 6d8ec4f

6 files changed

Lines changed: 604 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"binary-search-tree",
5656
"binary-search",
5757
"all-your-base",
58+
"custom-set",
5859
"wordy",
5960
"palindrome-products"
6061
],
@@ -314,6 +315,11 @@
314315
"difficulty": 1,
315316
"topics": []
316317
},
318+
{
319+
"slug": "custom-set",
320+
"difficulty": 1,
321+
"topics": []
322+
},
317323
{
318324
"slug": "wordy",
319325
"difficulty": 1,

‎exercises/custom-set/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
import java.util.Collection;
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import java.util.function.Predicate;
7+
import java.util.stream.Collectors;
8+
9+
public class CustomSet<T> {
10+
11+
private Set<T> set;
12+
13+
public CustomSet() {
14+
this(Collections.EMPTY_LIST);
15+
}
16+
17+
public CustomSet(Collection<T> data) {
18+
set = new HashSet<>(data.size());
19+
this.set.addAll(data);
20+
}
21+
22+
public boolean isEmpty() {
23+
return set.isEmpty();
24+
}
25+
26+
public boolean contains(T element) {
27+
return set.contains(element);
28+
}
29+
30+
public boolean isSubset(CustomSet<T> anotherSet) {
31+
return set.containsAll(anotherSet.set);
32+
}
33+
34+
public boolean isDisjoint(CustomSet<T> anotherSet) {
35+
if (set.isEmpty() || anotherSet.set.isEmpty()) {
36+
return true;
37+
}
38+
return set.stream()
39+
.filter(elem -> anotherSet.set.contains(elem))
40+
.count() == 0;
41+
}
42+
43+
public boolean equals(CustomSet<T> anotherSet) {
44+
return set.equals(anotherSet.set);
45+
}
46+
47+
public boolean add(T element) {
48+
return set.add(element);
49+
}
50+
51+
public CustomSet<T> getIntersection(CustomSet<T> anotherSet) {
52+
return new CustomSet<>(
53+
set.stream()
54+
.filter(anotherSet.set::contains)
55+
.collect(Collectors.toList())
56+
);
57+
}
58+
59+
public CustomSet<T> getUnion(CustomSet<T> anotherSet) {
60+
final Set<T> union = new HashSet<>(set);
61+
union.addAll(anotherSet.set);
62+
return new CustomSet<>(union);
63+
}
64+
65+
public CustomSet<T> getDifference(CustomSet<T> anotherSet) {
66+
final Predicate<T> predicate = anotherSet::contains;
67+
return new CustomSet<>(
68+
set.stream()
69+
.filter(predicate.negate())
70+
.collect(Collectors.toList())
71+
);
72+
}
73+
74+
}

‎exercises/custom-set/src/main/java/.keep‎

Whitespace-only changes.

0 commit comments

Comments
 (0)