Top Description Fields Methods
io.vavr.collection

public Interface Set<T>

extends Traversable<T>, Function1<T, Boolean>, Serializable
Known Direct Subinterfaces
io.vavr.collection.SortedSet
Known Direct Implementers
io.vavr.collection.HashSet, io.vavr.collection.LinkedHashSet
Type Parameters
<T>
Component type
Imports
io.vavr.Function1, .PartialFunction, .Tuple3, .Tuple2, io.vavr.control.Option, java.io.Serializable, java.util.Comparator, java.util.function.*

An immutable Set interface.

Caution

The Vavr Set implementations generally support null elements. However SortedSet implementations require an element Comparator, which may not support null elements.

Examples:

Set<?> addNull(Set<?> set) {

    // CAUTION: Do not expect a Set to accept null values in general!
    return set.add(null);

}

void test() {

    // ok
    addNull(HashSet.of(1));

    // ok
    addNull(TreeSet.of(nullsFirst(naturalOrder()), 1));

    // ok
    addNull(TreeSet.empty());

    // throws NPE!
    addNull(TreeSet.of(1));

}

Basic operations:

Conversion:
Author
Daniel Dietrich, Ruslan Sennov

Field Summary

Modifier and TypeField and Description
public static final long

Method Summary

Modifier and TypeMethod and Description
public Set<T>

Returns:

A new set containing all elements of this set and also element.
add
(T
The element to be added.
element
)

Add the given element to this set, if it is not already contained.

public Set<T>

Returns:

A new set containing all elements of this set and the given elements, if not already contained.
addAll
(Iterable<? extends T>
The elements to be added.
elements
)

Adds all of the given elements to this set, if not already contained.

public default Boolean

Returns:

true if the given element is contained, false otherwise.
apply
(T
the element to test for membership.
element
)

Implements io.vavr.Function1.apply.

Deprecated Will be removed

Tests if a given element is contained in this Set.

public <R> Set<R>
collect(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined of all elements of this traversable.
partialFunction
)

Redeclares io.vavr.collection.Traversable.collect.

Collects all elements that are in the domain of the given partialFunction by mapping the elements to type R.

public boolean
contains(T
An Object of type A, may be null.
element
)

Redeclares as abstract: io.vavr.Value.contains.

Shortcut for exists(e -> Objects.equals(e, element)), tests if the given element is contained.

public Set<T>

Returns:

A new Set containing all elements of this set which are not located in that set.
diff
(Set<? extends T>
Elements to be removed from this set.
that
)

Calculates the difference between this set and another set.

public Set<T>
distinct()

Redeclares io.vavr.collection.Traversable.distinct.

Returns a new version of this which contains no duplicates.

public Set<T>
distinctBy(Comparator<? super T>
A comparator
comparator
)

Redeclares io.vavr.collection.Traversable.distinctBy.

Returns a new version of this which contains no duplicates.

public <U> Set<T>
distinctBy(Function<? super T, ? extends U>
A key extractor
keyExtractor
)

Redeclares io.vavr.collection.Traversable.distinctBy.

Returns a new version of this which contains no duplicates.

public Set<T>
drop(int
The number of elements to drop.
n
)

Redeclares io.vavr.collection.Traversable.drop.

Drops the first n elements of this or all elements, if this length < n.

public Set<T>
dropRight(int
The number of elements to drop.
n
)

Redeclares io.vavr.collection.Traversable.dropRight.

Drops the last n elements of this or all elements, if this length < n.

public Set<T>
dropUntil(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Redeclares io.vavr.collection.Traversable.dropUntil.

Drops elements until the predicate holds for the current element.

public Set<T>
dropWhile(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Redeclares io.vavr.collection.Traversable.dropWhile.

Drops elements while the predicate holds for the current element.

public Set<T>
filter(Predicate<? super T>
A predicate
predicate
)

Redeclares io.vavr.collection.Traversable.filter.

Returns a new traversable consisting of all elements which satisfy the given predicate.

public <U> Set<U>
flatMap(Function<? super T, ? extends Iterable<? extends U>>
A mapper
mapper
)

Redeclares io.vavr.collection.Traversable.flatMap.

FlatMaps this Traversable.

public <C> Map<C, ? extends Set<T>>
groupBy(Function<? super T, ? extends C>
A function which classifies elements into classes
classifier
)

Redeclares io.vavr.collection.Traversable.groupBy.

Groups this elements by classifying the elements.

public Iterator<? extends Set<T>>
grouped(int
a positive block size
size
)

Redeclares io.vavr.collection.Traversable.grouped.

Groups this Traversable into fixed size blocks.

public Set<T>
init()

Redeclares io.vavr.collection.Traversable.init.

Dual of tail(), returning all elements except the last.

public Option<? extends Set<T>>
initOption()

Redeclares as abstract: io.vavr.collection.Traversable.initOption.

Dual of tailOption(), returning all elements except the last as Option.

public Set<T>

Returns:

A new Set consisting of all elements that are both in this set and in the given set that.
intersect
(Set<? extends T>
the set to intersect with.
that
)

Computes the intersection between this set and another set.

public default boolean
isDistinct()

Overrides default io.vavr.collection.Traversable.isDistinct.

Checks if this Traversable may consist of distinct elements only.

public Iterator<T>
iterator()

Redeclares as abstract: io.vavr.collection.Traversable.iterator.

An iterator by means of head() and tail().

public int
length()

Redeclares io.vavr.collection.Traversable.length.

Computes the number of elements of this Traversable.

public <U> Set<U>
map(Function<? super T, ? extends U>
A mapper.
mapper
)

Redeclares io.vavr.collection.Traversable.map.

Maps the elements of this Traversable to elements of a new type preserving their order, if any.

public static <
Component type of the Set.
T
>
Set<T>

Returns:

the given set instance as narrowed type Set<T>.
narrow
(Set<? extends T>
A Set.
set
)

Narrows a widened Set<?

public Set<T>
orElse(Iterable<? extends T>
An alternative Traversable
other
)

Redeclares io.vavr.collection.Traversable.orElse.

Returns this Traversable if it is nonempty, otherwise return the alternative.

public Set<T>
orElse(Supplier<? extends Iterable<? extends T>>
An alternative Traversable supplier
supplier
)

Redeclares io.vavr.collection.Traversable.orElse.

Returns this Traversable if it is nonempty, otherwise return the result of evaluating supplier.

public Tuple2<? extends Set<T>, ? extends Set<T>>
partition(Predicate<? super T>
A predicate which classifies an element if it is in the first or the second traversable.
predicate
)

Redeclares io.vavr.collection.Traversable.partition.

Creates a partition of this Traversable by splitting this elements in two in distinct traversables according to a predicate.

public Set<T>
peek(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Redeclares io.vavr.collection.Traversable.peek.

Performs the given action on the first element if this is an eager implementation.

public Set<T>
reject(Predicate<? super T>
A predicate
predicate
)

Redeclares as abstract: io.vavr.collection.Traversable.reject.

Returns a new traversable consisting of all elements which do not satisfy the given predicate.

public Set<T>

Returns:

A new set consisting of the elements of this set, without the given element.
remove
(T
The element to be removed from this set.
element
)

Removes a specific element from this set, if present.

public Set<T>

Returns:

A new set consisting of the elements of this set, without the given elements.
removeAll
(Iterable<? extends T>
The elements to be removed from this set.
elements
)

Removes all of the given elements from this set, if present.

public Set<T>
replace(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Redeclares io.vavr.collection.Traversable.replace.

Replaces the first occurrence (if exists) of the given currentElement with newElement.

public Set<T>
replaceAll(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Redeclares io.vavr.collection.Traversable.replaceAll.

Replaces all occurrences of the given currentElement with newElement.

public Set<T>
retainAll(Iterable<? extends T>
Elements to be kept.
elements
)

Redeclares io.vavr.collection.Traversable.retainAll.

Keeps all occurrences of the given elements from this.

public Set<T>
scan(T
neutral element for the operator op
zero
,
BiFunction<? super T, ? super T, ? extends T>
the associative operator for the scan
operation
)

Redeclares io.vavr.collection.Traversable.scan.

Computes a prefix scan of the elements of the collection.

public <U> Set<U>
scanLeft(U
the initial value
zero
,
BiFunction<? super U, ? super T, ? extends U>
the binary operator applied to the intermediate result and the element
operation
)

Redeclares io.vavr.collection.Traversable.scanLeft.

Produces a collection containing cumulative results of applying the operator going left to right.

public <U> Set<U>
scanRight(U
the initial value
zero
,
BiFunction<? super T, ? super U, ? extends U>
the binary operator applied to the intermediate result and the element
operation
)

Redeclares io.vavr.collection.Traversable.scanRight.

Produces a collection containing cumulative results of applying the operator going right to left.

public Iterator<? extends Set<T>>
slideBy(Function<? super T, ?>
A function which classifies elements into classes
classifier
)

Redeclares io.vavr.collection.Traversable.slideBy.

Slides a non-overlapping window of a variable size over this Traversable.

public Iterator<? extends Set<T>>
sliding(int
a positive window size
size
)

Redeclares io.vavr.collection.Traversable.sliding.

Slides a window of a specific size and step size 1 over this Traversable by calling sliding(int, int).

public Iterator<? extends Set<T>>
sliding(int
a positive window size
size
,
int
a positive step size
step
)

Redeclares io.vavr.collection.Traversable.sliding.

Slides a window of a specific size and step size over this Traversable.

public Tuple2<? extends Set<T>, ? extends Set<T>>
span(Predicate<? super T>
A predicate.
predicate
)

Redeclares io.vavr.collection.Traversable.span.

Returns a tuple where the first element is the longest prefix of elements that satisfy the given predicate and the second element is the remainder.

public Set<T>
tail()

Redeclares io.vavr.collection.Traversable.tail.

Drops the first element of a non-empty Traversable.

public Option<? extends Set<T>>
tailOption()

Redeclares io.vavr.collection.Traversable.tailOption.

Drops the first element of a non-empty Traversable and returns an Option.

public Set<T>
take(int
The number of elements to take.
n
)

Redeclares io.vavr.collection.Traversable.take.

Takes the first n elements of this or all elements, if this length < n.

public Set<T>
takeRight(int
The number of elements to take.
n
)

Redeclares io.vavr.collection.Traversable.takeRight.

Takes the last n elements of this or all elements, if this length < n.

public Set<T>
takeUntil(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Redeclares io.vavr.collection.Traversable.takeUntil.

Takes elements until the predicate holds for the current element.

public Set<T>
takeWhile(Predicate<? super T>
A condition tested subsequently for the contained elements.
predicate
)

Redeclares io.vavr.collection.Traversable.takeWhile.

Takes elements while the predicate holds for the current element.

public Set<T>

Returns:

a new java.util.Set instance
toJavaSet
()

Redeclares as abstract: io.vavr.Value.toJavaSet.

Converts this Vavr Set to a java.util.Set while preserving characteristics like insertion order (LinkedHashSet) and sort order (SortedSet).

public Set<T>

Returns:

A new set that contains all distinct elements of this and that set.
union
(Set<? extends T>
The set to form the union with.
that
)

Adds all of the elements of that set to this set, if not already present.

public <T1, T2> Tuple2<? extends Set<T1>, ? extends Set<T2>>
unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>>
a function which converts elements of this to pairs
unzipper
)

Redeclares io.vavr.collection.Traversable.unzip.

Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct sets.

public <T1, T2, T3> Tuple3<? extends Set<T1>, ? extends Set<T2>, ? extends Set<T3>>
unzip3(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>>
a function which converts elements of this to pairs
unzipper
)

Redeclares io.vavr.collection.Traversable.unzip3.

Unzips this elements by mapping this elements to triples which are subsequently split into three distinct sets.

public <U> Set<Tuple2<T, U>>
zip(Iterable<? extends U>
The Iterable providing the second half of each result pair.
that
)

Redeclares io.vavr.collection.Traversable.zip.

Returns a traversable formed from this traversable and another Iterable collection by combining corresponding elements in pairs.

public <U> Set<Tuple2<T, U>>
zipAll(Iterable<? extends U>
The Iterable providing the second half of each result pair.
that
,
T
The element to be used to fill up the result if this traversable is shorter than that.
thisElem
,
U
The element to be used to fill up the result if that is shorter than this traversable.
thatElem
)

Redeclares io.vavr.collection.Traversable.zipAll.

Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in pairs.

public <U, R> Set<R>
zipWith(Iterable<? extends U>
The Iterable providing the second parameter of the mapper.
that
,
BiFunction<? super T, ? super U, ? extends R>
a mapper.
mapper
)

Redeclares io.vavr.collection.Traversable.zipWith.

Returns a traversable formed from this traversable and another Iterable collection by mapping elements.

public Set<Tuple2<T, Integer>>
zipWithIndex()

Redeclares io.vavr.collection.Traversable.zipWithIndex.

Zips this traversable with its indices.

public <U> Set<U>
zipWithIndex(BiFunction<? super T, ? super Integer, ? extends U>
a mapper.
mapper
)

Redeclares io.vavr.collection.Traversable.zipWithIndex.

Zips this traversable with its indices by applying mapper provided.

Inherited from io.vavr.collection.Traversable:
arrangeByaveragecontainsAllcountequalsexistsUniquefindfindLastfoldLeftfoldRightforEachWithIndexgethasDefiniteSizehashCodeheadheadOptionisEmptyisOrderedisSequentialisSingleValuedisTraversableAgainlastlastOptionmaxmaxBymaxByminminByminBymkCharSeqmkCharSeqmkCharSeqmkStringmkStringmkStringnarrownonEmptyproductreduceLeftreduceLeftOptionreduceRightreduceRightOptionsinglesingleOptionsizespliteratorsum
Inherited from io.vavr.Function1:
andThenaritycomposeconstantcurriedidentityisMemoizedliftliftTrymemoizednarrowofpartialreversedtupled