All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.commons.collection.Sets Maven / Gradle / Ivy

There is a newer version: 12.0.2
Show newest version
package net.dongliu.commons.collection;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Utils for set
 *
 * @author Liu Dong
 */
public class Sets {

    /**
     * If set is null, return immutable empty set; else return self
     */
    public static  Set nullToEmpty(@Nullable Set set) {
        return set == null ? Sets.of() : set;
    }

    /**
     * Create immutable view of this Set.
     */
    public static  Set wrapImmutable(Set set) {
        return Collections.unmodifiableSet(set);
    }

    /**
     * Create a immutable set with content
     */
    public static  Set copyImmutable(Collection set) {
        return Collections.unmodifiableSet(new HashSet(set));
    }

    /**
     * Convert to other Set
     */
    public static  Set map(Collection set, Function mapper) {
        Set newSet = new HashSet<>((int) (set.size() / 0.75) + 1, 0.75f);
        for (T item : set) {
            newSet.add(mapper.apply(item));
        }
        return newSet;
    }

    /**
     * Get new set with items accepted by predicate
     */
    public static  Set filter(Set set, Predicate predicate) {
        return filter(() -> new HashSet<>((int) (set.size() / 0.75f) + 1, 0.75f), set, predicate);
    }

    /**
     * Get new set with items accepted by predicate
     */
    public static > E filter(Supplier supplier, Set set,
                                                 Predicate predicate) {
        E newSet = supplier.get();
        for (T value : set) {
            if (predicate.test(value)) {
                newSet.add(value);
            }
        }
        return newSet;
    }

    /**
     * Create mutable set, with expected size
     */
    public static  Set create(int expectedSize) {
        return new HashSet<>((int) (expectedSize / 0.75f) + 1, 0.75f);
    }

    /**
     * Create mutable set
     */
    public static  Set create() {
        return new HashSet<>();
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value) {
        Set set = new HashSet<>();
        set.add(value);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2, T value3) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        set.add(value3);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2, T value3, T value4) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2, T value3, T value4, T value5) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2, T value3, T value4, T value5, T value6) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        set.add(value6);
        return set;
    }

    /**
     * Create mutable set
     */
    public static  Set create(T value1, T value2, T value3, T value4, T value5, T value6, T value7) {
        Set set = new HashSet<>();
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        set.add(value6);
        set.add(value7);
        return set;
    }

    /**
     * create mutable set
     */
    @SafeVarargs
    public static  Set create(T... values) {
        Set set = new HashSet<>(values.length);
        Collections.addAll(set, values);
        return set;
    }


    /**
     * Create immutable set
     */
    public static  Set of() {
        return Collections.emptySet();
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value) {
        return Collections.singleton(value);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2) {
        Set set = create(2);
        set.add(value1);
        set.add(value2);
        return wrapImmutable(set);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2, T value3) {
        Set set = create(3);
        set.add(value1);
        set.add(value2);
        set.add(value3);
        return wrapImmutable(set);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2, T value3, T value4) {
        Set set = create(4);
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        return wrapImmutable(set);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2, T value3, T value4, T value5) {
        Set set = create(5);
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        return wrapImmutable(set);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2, T value3, T value4, T value5, T value6) {
        Set set = create(6);
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        set.add(value6);
        return wrapImmutable(set);
    }

    /**
     * Create immutable set
     */
    public static  Set of(T value1, T value2, T value3, T value4, T value5, T value6, T value7) {
        Set set = create(7);
        set.add(value1);
        set.add(value2);
        set.add(value3);
        set.add(value4);
        set.add(value5);
        set.add(value6);
        set.add(value7);
        return wrapImmutable(set);
    }

    /**
     * create immutable set
     */
    @SafeVarargs
    public static  Set of(T... values) {
        return wrapImmutable(create(values));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy