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

net.lenni0451.commons.collections.Sets Maven / Gradle / Ivy

The newest version!
package net.lenni0451.commons.collections;

import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.ApiStatus;

import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@UtilityClass
public class Sets {

    /**
     * Sort a set using the given comparator.
* This will return a {@link LinkedHashSet} to keep the order of the set. * * @param set The set to sort * @param comparator The comparator to use * @param The type of the set * @return The sorted set */ public static LinkedHashSet sort(final Set set, final Comparator comparator) { return set.stream() .sorted(comparator) .collect(Collectors.toCollection(LinkedHashSet::new)); } /** * Merge multiple sets into one.
* The sets are not modified. * * @param set The first set * @param others The other sets * @param The set type * @return The merged set */ @SafeVarargs public static Set merge(final Set set, final Set... others) { Set newSet = new HashSet<>(set); for (Set other : others) newSet.addAll(other); return newSet; } /** * Create a new set with the given objects. * * @param setSupplier The supplier for the set * @param objects The objects to add to the set * @param The set type * @param The object type * @return The created set */ @SafeVarargs public static , O> T any(final Supplier setSupplier, final O... objects) { T set = setSupplier.get(); Collections.addAll(set, objects); return set; } /** * Create a new set which is passed to the given consumer. * * @param setSupplier The supplier to create the set * @param setConsumer The consumer to pass the set to * @param The set type * @param The object type * @return The created set */ public static , O> T any(final Supplier setSupplier, final Consumer setConsumer) { T set = setSupplier.get(); setConsumer.accept(set); return set; } /** * Create a new {@link HashSet} with the given objects. * * @param objects The objects to add to the set * @param The object type * @return The created set */ @SafeVarargs public static HashSet hashSet(final T... objects) { return any(HashSet::new, objects); } /** * Create a new {@link HashSet} which is passed to the given consumer. * * @param setConsumer The consumer to pass the set to * @param The object type * @return The created set */ public static HashSet hashSet(final Consumer> setConsumer) { return any(HashSet::new, setConsumer); } /** * Create a new {@link LinkedHashSet} with the given objects. * * @param objects The objects to add to the set * @param The object type * @return The created set */ @SafeVarargs public static LinkedHashSet linkedHashSet(final T... objects) { return any(LinkedHashSet::new, objects); } /** * Create a new {@link LinkedHashSet} which is passed to the given consumer. * * @param setConsumer The consumer to pass the set to * @param The object type * @return The created set */ public static LinkedHashSet linkedHashSet(final Consumer> setConsumer) { return any(LinkedHashSet::new, setConsumer); } /** * Create a new {@link ConcurrentSkipListSet} with the given objects. * * @param objects The objects to add to the set * @param The object type * @return The created set */ @SafeVarargs public static ConcurrentSkipListSet concurrentSkipListSet(final T... objects) { return any(ConcurrentSkipListSet::new, objects); } /** * Create a new {@link ConcurrentSkipListSet} which is passed to the given consumer. * * @param setConsumer The consumer to pass the set to * @param The object type * @return The created set */ public static ConcurrentSkipListSet concurrentSkipListSet(final Consumer> setConsumer) { return any(ConcurrentSkipListSet::new, setConsumer); } /** * Deprecated - Use {@link #hashSet(Object[])} instead. */ @Deprecated @SafeVarargs @ApiStatus.ScheduledForRemoval public static HashSet newHashSet(final T... objects) { return hashSet(objects); } /** * Deprecated - Use {@link #hashSet(Consumer)} instead. */ @Deprecated @ApiStatus.ScheduledForRemoval public static HashSet newHashSet(final Consumer> setConsumer) { return hashSet(setConsumer); } /** * Deprecated - Use {@link #linkedHashSet(Object[])} instead. */ @Deprecated @SafeVarargs @ApiStatus.ScheduledForRemoval public static LinkedHashSet newLinkedHashSet(final T... objects) { return linkedHashSet(objects); } /** * Deprecated - Use {@link #linkedHashSet(Consumer)} instead. */ @Deprecated @ApiStatus.ScheduledForRemoval public static LinkedHashSet newLinkedHashSet(final Consumer> setConsumer) { return linkedHashSet(setConsumer); } /** * Deprecated - Use {@link #concurrentSkipListSet(Object[])} instead. */ @Deprecated @SafeVarargs @ApiStatus.ScheduledForRemoval public static ConcurrentSkipListSet newConcurrentSkipListSet(final T... objects) { return concurrentSkipListSet(objects); } /** * Deprecated - Use {@link #concurrentSkipListSet(Consumer)} instead. */ @Deprecated @ApiStatus.ScheduledForRemoval public static ConcurrentSkipListSet newConcurrentSkipListSet(final Consumer> setConsumer) { return concurrentSkipListSet(setConsumer); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy