/** * Returns a {@code Collector} that accumulates the input elements into a * new {@code Set}. There are no guarantees on the type, mutability, * serializability, or thread-safety of the {@code Set} returned; if more * control over the returned {@code Set} is required, use * {@link #toCollection(Supplier)}. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into a * {@code Set} */ public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, CH_UNORDERED_ID); }
/** * Returns a {@code Collector} that accumulates the input elements into a * new {@code Set}. There are no guarantees on the type, mutability, * serializability, or thread-safety of the {@code Set} returned; if more * control over the returned {@code Set} is required, use * {@link #toCollection(Supplier)}. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into a * {@code Set} */ public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, CH_UNORDERED_ID); }
/** * Returns a {@code Collector} that accumulates the input elements into an * {@link Sets#of(Object[]) unmodifiable Set}. The returned * Collector disallows null values and will throw {@code NullPointerException} * if it is presented with a null value. If the input contains duplicate elements, * an arbitrary element of the duplicates is preserved. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into an * <a href="../Sets.html#unmodifiable">unmodifiable Set</a> * @since 10 */ @SuppressWarnings("unchecked") public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, set -> (Set<T>) Sets.of(set.toArray()), CH_UNORDERED_NOID); }
/** * Returns a {@code Collector} that accumulates the input elements into an * {@link Sets#of(Object[]) unmodifiable Set}. The returned * Collector disallows null values and will throw {@code NullPointerException} * if it is presented with a null value. If the input contains duplicate elements, * an arbitrary element of the duplicates is preserved. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into an * <a href="../Sets.html#unmodifiable">unmodifiable Set</a> * @since 10 */ @SuppressWarnings("unchecked") public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, set -> (Set<T>) Sets.of(set.toArray()), CH_UNORDERED_NOID); }