Interface Set<A>
-
- Type Parameters:
A- the element type
- All Superinterfaces:
software.kes.enhancediterables.EnhancedIterable<A>,software.kes.enhancediterables.FiniteIterable<A>,com.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>,java.lang.Iterable<A>
- All Known Subinterfaces:
ImmutableNonEmptySet<A>,ImmutableSet<A>,NonEmptySet<A>
public interface Set<A> extends software.kes.enhancediterables.FiniteIterable<A>A finite, unordered view of a collection that contains no duplicate elements.A
Setguarantees the following:- A
size()method that executes in O(1). - A
contains(A)method that tests an value for membership in O(1). - An
isEmpty()method that executes in O(1). - Iteration will always terminate.
- Protected from mutation by the bearer.
- The bearer cannot gain access to a reference to the underlying collection.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <A> SetBuilder<A>builder()Creates a newSetBuilder.static <A> SetBuilder<A>builder(int initialCapacity)Creates a newSetBuilderwith an initial capacity hint.booleancontains(A element)Tests if an element is a member of thisSet.static <A> ImmutableSet<A>copyFrom(int maxCount, A[] source)Returns a newImmutableSetthat is copied from an array.static <A> ImmutableSet<A>copyFrom(int maxCount, java.lang.Iterable<A> source)Creates anImmutableSetthat is copied from anyIterable, but consuming a maximum number of elements.static <A> ImmutableSet<A>copyFrom(A[] source)Creates anImmutableSetthat is copied from an array.static <A> ImmutableSet<A>copyFrom(java.lang.Iterable<A> source)Creates anImmutableSetthat is copied from anyIterable.default Set<A>distinct()Since aSetalready contains only distinct values, this method always returns itself.static <A> ImmutableSet<A>empty()Returns an emptyImmutableSet.default com.jnape.palatable.lambda.adt.Maybe<A>find(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)Finds an element of thisSetthat satisfies a predicate, if any.default booleanisEmpty()Tests whether thisSetis empty.static <A> ImmutableNonEmptySet<A>of(A first, A... more)Creates aImmutableNonEmptySetwith the given elements.intsize()Returns the size of thisSet.default ImmutableSet<A>toImmutable()Converts thisSetto anImmutableSet.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>>toNonEmpty()Attempts to convert thisSetto aNonEmptySet.default NonEmptySet<A>toNonEmptyOrThrow()Attempts to convert thisSetto aNonEmptySet.static <A> Set<A>wrap(java.util.Set<A> underlying)Creates aSetthat wraps ajava.util.Set.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, take, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, concat, concat, cross, cross, cycle, drop, dropWhile, filter, fmap, foldLeft, foldRight, inits, intersperse, magnetizeBy, partition, prepend, prependAll, reverse, slide, span, tails, takeWhile, toFinite, zipWith
-
-
-
-
Method Detail
-
contains
boolean contains(A element)
Tests if an element is a member of thisSet.- Parameters:
element- the element to test- Returns:
- true if
elementis a member of thisSet, false otherwise
-
size
int size()
Returns the size of thisSet.Executes in O(1).
-
distinct
default Set<A> distinct()
Since aSetalready contains only distinct values, this method always returns itself.- Specified by:
distinctin interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Returns:
- itself
-
find
default com.jnape.palatable.lambda.adt.Maybe<A> find(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Finds an element of thisSetthat satisfies a predicate, if any.- Specified by:
findin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Parameters:
predicate- a predicate; not null- Returns:
- an element wrapped in a
Maybe.just(A)if a matching element is found;Maybe.nothing()otherwise.
-
isEmpty
default boolean isEmpty()
Tests whether thisSetis empty.Executes in O(1).
-
toImmutable
default ImmutableSet<A> toImmutable()
Converts thisSetto anImmutableSet. Converts thisSetto anImmutableSet.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
Setis already anImmutableSet, no copies are made and this method is a no-op.- Returns:
- an
ImmutableSetof the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>> toNonEmpty()
Attempts to convert thisSetto aNonEmptySet.If successful, returns a
NonEmptySetcontaining the same elements as this one, wrapped in aMaybe.just(A).If this
Setis empty, returnsMaybe.nothing().Does not make copies of any underlying data structures.
-
toNonEmptyOrThrow
default NonEmptySet<A> toNonEmptyOrThrow()
Attempts to convert thisSetto aNonEmptySet.If successful, returns a
NonEmptySetcontaining the same elements as this one. Use this if you are confident that thisSetis not empty.If this
Setis empty, throws anIllegalArgumentException.Does not make copies of any underlying data structures.
- Returns:
- a
NonEmptySet<A> - Throws:
java.lang.IllegalArgumentException- if thisSetis empty
-
empty
static <A> ImmutableSet<A> empty()
Returns an emptyImmutableSet.- Type Parameters:
A- the element type- Returns:
- an empty
ImmutableSet<A>
-
of
@SafeVarargs static <A> ImmutableNonEmptySet<A> of(A first, A... more)
Creates aImmutableNonEmptySetwith the given elements.- Type Parameters:
A- the element type- Parameters:
first- the first elementmore- the remaining elements- Returns:
- an
ImmutableNonEmptySet<A>
-
builder
static <A> SetBuilder<A> builder()
Creates a newSetBuilder.- Type Parameters:
A- the element type- Returns:
- an empty
SetBuilder
-
builder
static <A> SetBuilder<A> builder(int initialCapacity)
Creates a newSetBuilderwith an initial capacity hint.- Type Parameters:
A- the element type- Parameters:
initialCapacity- an initial capacity hint. Must be >= 0.- Returns:
- an empty
SetBuilder
-
wrap
static <A> Set<A> wrap(java.util.Set<A> underlying)
Creates aSetthat wraps ajava.util.Set.Does not make any copies of the given
Set. The createdSetwill hold a reference to the given underlyingSet, but will not alter it in any way.Bearers of the created
Setwill be unable to gain access to the underlyingSet, so it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the underlying
Setcan still mutate it. Mutating the underlyingSetis not advised. Operations that change the size of the underlyingSetwill result in unpredictable behavior. UsecopyFrom(java.lang.Iterable<A>)if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying-Setto wrap; not null- Returns:
- a
Set<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(java.lang.Iterable<A> source)
Creates anImmutableSetthat is copied from anyIterable.The entire
Iterablewill be eagerly iterated. Be careful not to pass in an infiniteIterableor this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
sourcealready is anImmutableSet, it will be returned directly.- Type Parameters:
A- the element type- Parameters:
source- anIterable<A>that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableSet<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(A[] source)
Creates anImmutableSetthat is copied from an array.- Type Parameters:
A- the element type- Parameters:
source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableSet<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableSetthat is copied from anyIterable, but consuming a maximum number of elements.The
Iterablewill be eagerly iterated, but only up to a maximum ofmaxCountelements. IfmaxCountelements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
sourceis anImmutableSetand its size equal tomaxCount, in which case it will be returned directly.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to consume from the source. Must be >= 0.source- anIterable<A>that will be iterated eagerly for up tomaxCountelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableSetthat contains at mostmaxCountelements
-
copyFrom
static <A> ImmutableSet<A> copyFrom(int maxCount, A[] source)
Returns a newImmutableSetthat is copied from an array.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to copy from the array. Must be >= 0.source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableSet<A>
-
-