T - Component typepublic interface Set<T> extends Traversable<T>, java.io.Serializable
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:| Modifier and Type | Field and Description |
|---|---|
static long |
serialVersionUID |
| Modifier and Type | Method and Description |
|---|---|
Set<T> |
add(T element)
Add the given element to this set, if it is not already contained.
|
Set<T> |
addAll(java.lang.Iterable<? extends T> elements)
Adds all of the given elements to this set, if not already contained.
|
<R> Set<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects all elements that are in the domain of the given
partialFunction by mapping the elements to type R. |
boolean |
contains(T element)
Shortcut for
exists(e -> Objects.equals(e, element)), tests if the given element is contained. |
Set<T> |
diff(Set<? extends T> that)
Calculates the difference between this set and another set.
|
Set<T> |
distinct()
Returns a new version of this which contains no duplicates.
|
Set<T> |
distinctBy(java.util.Comparator<? super T> comparator)
Returns a new version of this which contains no duplicates.
|
<U> Set<T> |
distinctBy(java.util.function.Function<? super T,? extends U> keyExtractor)
Returns a new version of this which contains no duplicates.
|
Set<T> |
drop(int n)
Drops the first n elements of this or all elements, if this length < n.
|
Set<T> |
dropRight(int n)
Drops the last n elements of this or all elements, if this length < n.
|
Set<T> |
dropUntil(java.util.function.Predicate<? super T> predicate)
Drops elements until the predicate holds for the current element.
|
Set<T> |
dropWhile(java.util.function.Predicate<? super T> predicate)
Drops elements while the predicate holds for the current element.
|
Set<T> |
filter(java.util.function.Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which satisfy the given predicate.
|
Set<T> |
filterNot(java.util.function.Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which do not satisfy the given predicate.
|
<U> Set<U> |
flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
FlatMaps this Traversable.
|
<C> Map<C,? extends Set<T>> |
groupBy(java.util.function.Function<? super T,? extends C> classifier)
Groups this elements by classifying the elements.
|
Iterator<? extends Set<T>> |
grouped(int size)
Groups this
Traversable into fixed size blocks. |
Set<T> |
init()
Dual of Traversable.tail(), returning all elements except the last.
|
Option<? extends Set<T>> |
initOption()
Dual of Traversable.tailOption(), returning all elements except the last as
Option. |
Set<T> |
intersect(Set<? extends T> that)
Computes the intersection between this set and another set.
|
default boolean |
isDistinct()
Checks if this Traversable may consist of distinct elements only.
|
Iterator<T> |
iterator()
An iterator by means of head() and tail().
|
int |
length()
Computes the number of elements of this Traversable.
|
<U> Set<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the elements of this
Traversable to elements of a new type preserving their order, if any. |
static <T> Set<T> |
narrow(Set<? extends T> set)
Narrows a widened
Set<? extends T> to Set<T>
by performing a type-safe cast. |
Set<T> |
orElse(java.lang.Iterable<? extends T> other)
Returns this
Traversable if it is nonempty, otherwise return the alternative. |
Set<T> |
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Returns this
Traversable if it is nonempty, otherwise return the result of evaluating supplier. |
Tuple2<? extends Set<T>,? extends Set<T>> |
partition(java.util.function.Predicate<? super T> predicate)
Creates a partition of this
Traversable by splitting this elements in two in distinct traversables
according to a predicate. |
Set<T> |
peek(java.util.function.Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
Set<T> |
remove(T element)
Removes a specific element from this set, if present.
|
Set<T> |
removeAll(java.lang.Iterable<? extends T> elements)
Removes all of the given elements from this set, if present.
|
Set<T> |
replace(T currentElement,
T newElement)
Replaces the first occurrence (if exists) of the given currentElement with newElement.
|
Set<T> |
replaceAll(T currentElement,
T newElement)
Replaces all occurrences of the given currentElement with newElement.
|
Set<T> |
retainAll(java.lang.Iterable<? extends T> elements)
Keeps all occurrences of the given elements from this.
|
Set<T> |
scan(T zero,
java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Computes a prefix scan of the elements of the collection.
|
<U> Set<U> |
scanLeft(U zero,
java.util.function.BiFunction<? super U,? super T,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going left to right.
|
<U> Set<U> |
scanRight(U zero,
java.util.function.BiFunction<? super T,? super U,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going right to left.
|
Iterator<? extends Set<T>> |
slideBy(java.util.function.Function<? super T,?> classifier)
Slides a non-overlapping window of a variable size over this
Traversable. |
Iterator<? extends Set<T>> |
sliding(int size)
Slides a window of a specific
size and step size 1 over this Traversable by calling
Traversable.sliding(int, int). |
Iterator<? extends Set<T>> |
sliding(int size,
int step)
Slides a window of a specific
size and step size over this Traversable. |
Tuple2<? extends Set<T>,? extends Set<T>> |
span(java.util.function.Predicate<? super T> predicate)
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. |
Set<T> |
tail()
Drops the first element of a non-empty Traversable.
|
Option<? extends Set<T>> |
tailOption()
Drops the first element of a non-empty Traversable and returns an
Option. |
Set<T> |
take(int n)
Takes the first n elements of this or all elements, if this length < n.
|
Set<T> |
takeRight(int n)
Takes the last n elements of this or all elements, if this length < n.
|
Set<T> |
takeUntil(java.util.function.Predicate<? super T> predicate)
Takes elements until the predicate holds for the current element.
|
Set<T> |
takeWhile(java.util.function.Predicate<? super T> predicate)
Takes elements while the predicate holds for the current element.
|
java.util.Set<T> |
toJavaSet()
Converts this Vavr
Set to a java.util.Set while preserving characteristics
like insertion order (LinkedHashSet) and sort order (SortedSet). |
Set<T> |
union(Set<? extends T> that)
Adds all of the elements of
that set to this set, if not already present. |
<U> Set<Tuple2<T,U>> |
zip(java.lang.Iterable<? extends U> that)
Returns a traversable formed from this traversable and another Iterable collection by combining
corresponding elements in pairs.
|
<U> Set<Tuple2<T,U>> |
zipAll(java.lang.Iterable<? extends U> that,
T thisElem,
U thatElem)
Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in
pairs.
|
<U,R> Set<R> |
zipWith(java.lang.Iterable<? extends U> that,
java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Returns a traversable formed from this traversable and another Iterable collection by mapping elements.
|
Set<Tuple2<T,java.lang.Integer>> |
zipWithIndex()
Zips this traversable with its indices.
|
<U> Set<U> |
zipWithIndex(java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
Zips this traversable with its indices by applying mapper provided.
|
arrangeBy, average, containsAll, count, equals, existsUnique, find, findLast, fold, foldLeft, foldRight, forEachWithIndex, get, hasDefiniteSize, hashCode, head, headOption, isEmpty, isOrdered, isSequential, isSingleValued, isTraversableAgain, last, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, narrow, nonEmpty, product, reduce, reduceLeft, reduceLeftOption, reduceOption, reduceRight, reduceRightOption, single, singleOption, size, spliterator, sum, toLinkedMap, toMap, toSortedMap, toSortedMap, unzip, unzip3collect, collect, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, isAsync, isLazy, narrow, out, out, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVectorstatic final long serialVersionUID
static <T> Set<T> narrow(Set<? extends T> set)
Set<? extends T> to Set<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T - Component type of the Set.set - A Set.set instance as narrowed type Set<T>.Set<T> add(T element)
element - The element to be added.element.Set<T> addAll(java.lang.Iterable<? extends T> elements)
elements - The elements to be added.elements, if not already contained.Set<T> diff(Set<? extends T> that)
See also removeAll(Iterable).
that - Elements to be removed from this set.that set.Set<T> intersect(Set<? extends T> that)
See also retainAll(Iterable).
that - the set to intersect with.that.Set<T> remove(T element)
element - The element to be removed from this set.element.Set<T> removeAll(java.lang.Iterable<? extends T> elements)
elements - The elements to be removed from this set.elements.java.util.Set<T> toJavaSet()
Set to a java.util.Set while preserving characteristics
like insertion order (LinkedHashSet) and sort order (SortedSet).Set<T> union(Set<? extends T> that)
that set to this set, if not already present.
See also addAll(Iterable).
that - The set to form the union with.that set.<R> Set<R> collect(PartialFunction<? super T,? extends R> partialFunction)
TraversablepartialFunction by mapping the elements to type R.
More specifically, for each of this elements in iteration order first it is checked
partialFunction.isDefinedAt(element)
If the elements makes it through that filter, the mapped instance is added to the result collection
R newElement = partialFunction.apply(element)
Note:If this Traversable is ordered (i.e. extends Ordered,
the caller of collect has to ensure that the elements are comparable (i.e. extend Comparable).collect in interface Traversable<T>R - The new element typepartialFunction - A function that is not necessarily defined of all elements of this traversable.Traversable instance containing elements of type Rboolean contains(T element)
Valueexists(e -> Objects.equals(e, element)), tests if the given element is contained.Set<T> distinct()
Traversableequals.distinct in interface Traversable<T>Traversable containing this elements without duplicatesSet<T> distinctBy(java.util.Comparator<? super T> comparator)
Traversablecomparator.distinctBy in interface Traversable<T>comparator - A comparatorTraversable containing this elements without duplicates<U> Set<T> distinctBy(java.util.function.Function<? super T,? extends U> keyExtractor)
Traversableequals.
The elements of the result are determined in the order of their occurrence - first match wins.
distinctBy in interface Traversable<T>U - key typekeyExtractor - A key extractorTraversable containing this elements without duplicatesSet<T> drop(int n)
Traversabledrop in interface Traversable<T>n - The number of elements to drop.Set<T> dropRight(int n)
TraversabledropRight in interface Traversable<T>n - The number of elements to drop.Set<T> dropUntil(java.util.function.Predicate<? super T> predicate)
TraversabledropUntil in interface Traversable<T>predicate - A condition tested subsequently for this elements.Set<T> dropWhile(java.util.function.Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as dropUntil(predicate.negate()).
It is intended to be used with method references, which cannot be negated directly.
dropWhile in interface Traversable<T>predicate - A condition tested subsequently for this elements.Set<T> filter(java.util.function.Predicate<? super T> predicate)
Traversablefilter in interface Traversable<T>predicate - A predicateSet<T> filterNot(java.util.function.Predicate<? super T> predicate)
TraversableThe default implementation is equivalent to
filter(predicate.negate()filterNot in interface Traversable<T>predicate - A predicate<U> Set<U> flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
TraversableflatMap in interface Traversable<T>U - The resulting component type.mapper - A mapper<C> Map<C,? extends Set<T>> groupBy(java.util.function.Function<? super T,? extends C> classifier)
TraversablegroupBy in interface Traversable<T>C - classified class typeclassifier - A function which classifies elements into classesTraversable.arrangeBy(Function)Iterator<? extends Set<T>> grouped(int size)
TraversableTraversable into fixed size blocks.
Let length be the length of this Iterable. Then grouped is defined as follows:
this.isEmpty(), the resulting Iterator is empty.size <= length, the resulting Iterator will contain length / size blocks of size
size and maybe a non-empty block of size length % size, if there are remaining elements.size > length, the resulting Iterator will contain one block of size length.
[].grouped(1) = []
[].grouped(0) throws
[].grouped(-1) throws
[1,2,3,4].grouped(2) = [[1,2],[3,4]]
[1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]]
[1,2,3,4].grouped(5) = [[1,2,3,4]]
Please note that grouped(int) is a special case of Traversable.sliding(int, int), i.e.
grouped(size) is the same as sliding(size, size).grouped in interface Traversable<T>size - a positive block sizeSet<T> init()
Traversableinit in interface Traversable<T>Option<? extends Set<T>> initOption()
TraversableOption.initOption in interface Traversable<T>Some(traversable) or None if this is empty.default boolean isDistinct()
TraversableisDistinct in interface Traversable<T>Iterator<T> iterator()
Traversableint length()
Traversable
Same as Traversable.size().
length in interface Traversable<T><U> Set<U> map(java.util.function.Function<? super T,? extends U> mapper)
TraversableTraversable to elements of a new type preserving their order, if any.Set<T> orElse(java.lang.Iterable<? extends T> other)
TraversableTraversable if it is nonempty, otherwise return the alternative.orElse in interface Traversable<T>other - An alternative TraversableTraversable if it is nonempty, otherwise return the alternative.Set<T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
TraversableTraversable if it is nonempty, otherwise return the result of evaluating supplier.orElse in interface Traversable<T>supplier - An alternative Traversable supplierTraversable if it is nonempty, otherwise return the result of evaluating supplier.Tuple2<? extends Set<T>,? extends Set<T>> partition(java.util.function.Predicate<? super T> predicate)
TraversableTraversable by splitting this elements in two in distinct traversables
according to a predicate.partition in interface Traversable<T>predicate - A predicate which classifies an element if it is in the first or the second traversable.Traversable contains all elements that satisfy the given predicate, the second Traversable contains all elements that don't. The original order of elements is preserved.Set<T> peek(java.util.function.Consumer<? super T> action)
Valueaction on the first element if this is an eager implementation.
Performs the given action on all elements (the first immediately, successive deferred),
if this is a lazy implementation.Set<T> replace(T currentElement, T newElement)
Traversablereplace in interface Traversable<T>currentElement - An element to be substituted.newElement - A replacement for currentElement.Set<T> replaceAll(T currentElement, T newElement)
TraversablereplaceAll in interface Traversable<T>currentElement - An element to be substituted.newElement - A replacement for currentElement.Set<T> retainAll(java.lang.Iterable<? extends T> elements)
TraversableretainAll in interface Traversable<T>elements - Elements to be kept.Set<T> scan(T zero, java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Traversablescan in interface Traversable<T>zero - neutral element for the operator opoperation - the associative operator for the scan<U> Set<U> scanLeft(U zero, java.util.function.BiFunction<? super U,? super T,? extends U> operation)
TraversablescanLeft in interface Traversable<T>U - the type of the elements in the resulting collectionzero - the initial valueoperation - the binary operator applied to the intermediate result and the element<U> Set<U> scanRight(U zero, java.util.function.BiFunction<? super T,? super U,? extends U> operation)
TraversablescanRight in interface Traversable<T>U - the type of the elements in the resulting collectionzero - the initial valueoperation - the binary operator applied to the intermediate result and the elementIterator<? extends Set<T>> slideBy(java.util.function.Function<? super T,?> classifier)
TraversableTraversable.
Each window contains elements with the same class, as determined by classifier. Two consecutive
values in this Traversable will be in the same window only if classifier returns equal
values for them. Otherwise, the values will constitute the last element of the previous window and the
first element of the next window.
Examples:
[].slideBy(Function.identity()) = []
[1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]]
[1,2,3,10,12,5,7,20,29].slideBy(x -> x/10) = [[1,2,3],[10,12],[5,7],[20,29]]
slideBy in interface Traversable<T>classifier - A function which classifies elements into classesIterator<? extends Set<T>> sliding(int size)
Traversablesize and step size 1 over this Traversable by calling
Traversable.sliding(int, int).sliding in interface Traversable<T>size - a positive window sizeIterator<? extends Set<T>> sliding(int size, int step)
Traversablesize and step size over this Traversable.
Examples:
[].sliding(1,1) = []
[1,2,3,4,5].sliding(2,3) = [[1,2],[4,5]]
[1,2,3,4,5].sliding(2,4) = [[1,2],[5]]
[1,2,3,4,5].sliding(2,5) = [[1,2]]
[1,2,3,4].sliding(5,3) = [[1,2,3,4]]
sliding in interface Traversable<T>size - a positive window sizestep - a positive step sizeTuple2<? extends Set<T>,? extends Set<T>> span(java.util.function.Predicate<? super T> predicate)
Traversablepredicate and the second element is the remainder.span in interface Traversable<T>predicate - A predicate.Tuple containing the longest prefix of elements that satisfy p and the remainder.Set<T> tail()
Traversabletail in interface Traversable<T>Option<? extends Set<T>> tailOption()
TraversableOption.tailOption in interface Traversable<T>Some(traversable) or None if this is empty.Set<T> take(int n)
Traversable
The result is equivalent to sublist(0, max(0, min(length(), n))) but does not throw if n < 0 or
n > length().
In the case of n < 0 the empty instance is returned, in the case of n > length() this is returned.
take in interface Traversable<T>n - The number of elements to take.Set<T> takeRight(int n)
Traversable
The result is equivalent to sublist(max(0, min(length(), length() - n)), n), i.e. takeRight will not
throw if n < 0 or n > length().
In the case of n < 0 the empty instance is returned, in the case of n > length() this is returned.
takeRight in interface Traversable<T>n - The number of elements to take.Set<T> takeUntil(java.util.function.Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as takeWhile(predicate.negate()). It is intended to be used with
method references, which cannot be negated directly.
takeUntil in interface Traversable<T>predicate - A condition tested subsequently for this elements.Set<T> takeWhile(java.util.function.Predicate<? super T> predicate)
TraversabletakeWhile in interface Traversable<T>predicate - A condition tested subsequently for the contained elements.<U> Set<Tuple2<T,U>> zip(java.lang.Iterable<? extends U> that)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zip in interface Traversable<T>U - The type of the second half of the returned pairs.that - The Iterable providing the second half of each result pair.that iterable.<U,R> Set<R> zipWith(java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zipWith in interface Traversable<T>U - The type of the second parameter of the mapper.R - The type of the mapped elements.that - The Iterable providing the second parameter of the mapper.mapper - a mapper.that iterable.<U> Set<Tuple2<T,U>> zipAll(java.lang.Iterable<? extends U> that, T thisElem, U thatElem)
Traversable
The length of the returned traversable is the maximum of the lengths of this traversable and that
iterable.
Special case: if this traversable is shorter than that elements, and that elements contains duplicates, the resulting traversable may be shorter than the maximum of the lengths of this and that because a traversable contains an element at most once.
If this Traversable is shorter than that, thisElem values are used to fill the result. If that is shorter than this Traversable, thatElem values are used to fill the result.
zipAll in interface Traversable<T>U - The type of the second half of the returned pairs.that - The Iterable providing the second half of each result pair.thisElem - The element to be used to fill up the result if this traversable is shorter than that.thatElem - The element to be used to fill up the result if that is shorter than this traversable.Set<Tuple2<T,java.lang.Integer>> zipWithIndex()
TraversablezipWithIndex in interface Traversable<T><U> Set<U> zipWithIndex(java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
TraversablezipWithIndex in interface Traversable<T>U - The type of the mapped elements.mapper - a mapper.