Sha256: d2cc026e0df2ec664344ad7495443d253386d039d5470f3a2c0fcbe164b4fce2

Contents?: true

Size: 1.87 KB

Versions: 194

Compression:

Stored size: 1.87 KB

Contents

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())
        );
    }

}

Version data entries

194 entries across 194 versions & 1 rubygems

Version Path
trackler-2.2.1.18 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.17 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.16 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.15 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.14 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.13 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.12 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.11 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.10 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.9 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.8 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.7 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.6 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.5 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.4 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.3 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.2 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.1 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.1.0 tracks/java/exercises/custom-set/src/example/java/CustomSet.java
trackler-2.2.0.6 tracks/java/exercises/custom-set/src/example/java/CustomSet.java