Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"binary-search-tree",
"binary-search",
"all-your-base",
"custom-set",
"wordy",
"palindrome-products"
],
Expand Down Expand Up @@ -314,6 +315,11 @@
"difficulty": 1,
"topics": []
},
{
"slug": "custom-set",
"difficulty": 1,
"topics": []
},
{
"slug": "wordy",
"difficulty": 1,
Expand Down
18 changes: 18 additions & 0 deletions exercises/custom-set/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
74 changes: 74 additions & 0 deletions exercises/custom-set/src/example/java/CustomSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CustomSet<T> {

private Set<T> set;

public CustomSet() {
this(Collections.EMPTY_LIST);
}

public CustomSet(Collection<T> data) {
set = new HashSet<>(data.size());
this.set.addAll(data);
}

public boolean isEmpty() {
return set.isEmpty();
}

public boolean contains(T element) {
return set.contains(element);
}

public boolean isSubset(CustomSet<T> anotherSet) {
return set.containsAll(anotherSet.set);
}

public boolean isDisjoint(CustomSet<T> anotherSet) {
if (set.isEmpty() || anotherSet.set.isEmpty()) {
return true;
}
return set.stream()
.filter(elem -> anotherSet.set.contains(elem))
.count() == 0;
}

public boolean equals(CustomSet<T> anotherSet) {
return set.equals(anotherSet.set);
}

public boolean add(T element) {
return set.add(element);
}

public CustomSet<T> getIntersection(CustomSet<T> anotherSet) {
return new CustomSet<>(
set.stream()
.filter(anotherSet.set::contains)
.collect(Collectors.toList())
);
}

public CustomSet<T> getUnion(CustomSet<T> anotherSet) {
final Set<T> union = new HashSet<>(set);
union.addAll(anotherSet.set);
return new CustomSet<>(union);
}

public CustomSet<T> getDifference(CustomSet<T> anotherSet) {
final Predicate<T> predicate = anotherSet::contains;
return new CustomSet<>(
set.stream()
.filter(predicate.negate())
.collect(Collectors.toList())
);
}

}
Empty file.
Loading