Code Smell: Remove duplicate logic for searching in sets
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
aef1acf43e
commit
e4f1670742
5 changed files with 26 additions and 9 deletions
|
@ -1,17 +1,19 @@
|
|||
package datastructures.set;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface CustomSet<T> {
|
||||
|
||||
|
||||
|
||||
Stream<T> stream();
|
||||
|
||||
boolean add(T value);
|
||||
|
||||
boolean remove(T value);
|
||||
|
||||
Optional<T> find(Predicate<T> predicate);
|
||||
|
||||
Set<T> getSet();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package datastructures.set;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CustomStrictSet<T> implements CustomSet<T> {
|
||||
|
@ -16,6 +18,11 @@ public class CustomStrictSet<T> implements CustomSet<T> {
|
|||
return this.set.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> find(Predicate<T> predicate) {
|
||||
return set.stream().filter(predicate).findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> getSet() {
|
||||
return this.set;
|
||||
|
|
Reference in a new issue