-
- Type Parameters:
T
- the entity type contained in the set
- All Superinterfaces:
Collection<T>
,Foldable<T>
,java.lang.Iterable<T>
,List<T>
,java.io.Serializable
,Streamable<T>
,Traversable<T>
,Value<T>
public interface Set<T> extends List<T>
The set is an extension of theCollection
interface that guarantees only unique elements are contained within the set. How uniqueness is guaranteed varies pending the implementing class. Currently the following implementations are supported:Collections.Set(Object[])
, an implementation that uses the entities hashCollections.SortedSet()
, a set where all elements are sorted based on aComparator
Single change operations Operation Description append(Object) Add element to end of the sequence remove(int) Remove an element by its index Collection based operations Operation Description complement(Iterable)
complement(Iterable[])Creates a set with elements only contained in this intersect(Iterable)
intersect(Iterable[])Creates a set containing elements that are both in this and the provided iterables map(Function) Create a new sequence with the mapped values filter(Predicate) Create a new set with values matching the predicate reject(Predicate) Create a new sequence without the rejected values matching the predicate union(Iterable) Combine this sequence of elements with the provided iterable - Since:
- 0.0.3
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Set<T>
append(T value)
Create a new list containing all elements in this instance and appending the providedvalue
to the end of the new list.default Set<T>
complement(java.lang.Iterable<T> iterable)
Creates a new set that contains elements that are only inthis
, but not contained withiniterable
.Set<T>
complement(java.lang.Iterable<T>... iterables)
Creates a new set that contains elements that are only inthis
, but not contained within any of theiterables
.Set<T>
distinctBy(java.util.Comparator<T> comparator)
Creates a distinct list based upon the provided comparator.Set<T>
filter(java.util.function.Predicate<T> predicate)
Filter out an element if it does not match the suppliedpredicate
.default T
head()
Fetch the head of the collection and return it.default Set<T>
intersect(java.lang.Iterable<T> iterable)
Creates a set that contains only the elements that are in boththis
and the providediterable
.Set<T>
intersect(java.lang.Iterable<T>... iterables)
Creates a set that contains only the elements that are in all collections andthis
.<U> Set<U>
map(java.util.function.Function<T,U> mapper)
Perform a mapping operation on the elements in the stream.Set<T>
orElse(java.lang.Iterable<? extends T> other)
Returns eitherthis
if it is non empty, otherwise will return the providedother
.Set<T>
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.default Set<T>
reject(java.util.function.Predicate<T> predicate)
Return a list that removes all elements that match thepredicate
provided.Set<T>
remove(int index)
Removes an element from the list and returns a new instance of the list.Set<T>
tail()
Build a new collection with all elements except for the head.java.util.Set<T>
toJava()
Transform this collection into one supported natively in Java.Set<T>
union(java.lang.Iterable<T> iterable)
Create a new sequence with all elements of this sequence combined with the elements of the provided iterable.-
Methods inherited from interface com.jongsoft.lang.collection.Collection
containsAll, count, first, foldLeft, foldRight, get, isEmpty, isSingleValued, last, reduceLeft, size, split, summing
-
Methods inherited from interface com.jongsoft.lang.collection.List
firstIndexWhere, get, groupBy, indexOf, median, pipeline, sorted
-
Methods inherited from interface com.jongsoft.lang.collection.Traversable
average, iterator, sum
-
-
-
-
Method Detail
-
append
Set<T> append(T value)
Description copied from interface:List
Create a new list containing all elements in this instance and appending the providedvalue
to the end of the new list.Example:
// will result in a list with 2, 3, 4, 1 List(2, 3, 4).append(1)
-
distinctBy
Set<T> distinctBy(java.util.Comparator<T> comparator)
Description copied from interface:List
Creates a distinct list based upon the provided comparator. If a duplicate is found only the first match will be included in the returned list.- Specified by:
distinctBy
in interfaceList<T>
- Parameters:
comparator
- the comparator to use for distinct check- Returns:
- the distinct list
-
complement
default Set<T> complement(java.lang.Iterable<T> iterable)
Creates a new set that contains elements that are only inthis
, but not contained withiniterable
.Example:
// the example would be a Set(1, 2) Set result = Set(1, 2, 3).complement(Set(3, 4));
- Parameters:
iterable
- the iterable to perform the complement with- Returns:
- the product of the complement operation
-
complement
Set<T> complement(java.lang.Iterable<T>... iterables)
Creates a new set that contains elements that are only inthis
, but not contained within any of theiterables
.Example:
// the example would be a Set(1, 2) Set result = Set(1, 2, 3).complement(Set(3, 4));
- Parameters:
iterables
- the iterables to perform the complement with- Returns:
- the product of the complement operation
-
filter
Set<T> filter(java.util.function.Predicate<T> predicate)
Description copied from interface:Streamable
Filter out an element if it does not match the suppliedpredicate
. This operation will iterate over all elements and return a new set containing only the elements where the predicate returnstrue
for.- Specified by:
filter
in interfaceCollection<T>
- Specified by:
filter
in interfaceList<T>
- Specified by:
filter
in interfaceStreamable<T>
- Specified by:
filter
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to apply to the contents of this- Returns:
- the filtered value
-
head
default T head()
Description copied from interface:Collection
Fetch the head of the collection and return it. The following operations will all return the same value for a non empty collection.
The caller must verify the collection is not empty using either Collection.size() or Collection.isEmpty() to prevent aCollection(0, 1).get(); Collection(0, 1).head();
NoSuchElementException
to be thrown.- Specified by:
head
in interfaceCollection<T>
- Returns:
- the first element in the collection
-
intersect
default Set<T> intersect(java.lang.Iterable<T> iterable)
Creates a set that contains only the elements that are in boththis
and the providediterable
.Example:
// the example would be a Set(3) Set result = Set(1, 2, 3).intersect(Set(3, 4));
- Parameters:
iterable
- the iterable to perform the intersects with- Returns:
- the product of the intersect operation
-
intersect
Set<T> intersect(java.lang.Iterable<T>... iterables)
Creates a set that contains only the elements that are in all collections andthis
.We say that A intersects (meets) B at an element x if x belongs to A and B. We say that A intersects (meets) B if A intersects B at some element. A intersects B if their intersection is inhabited.
Example:
// the example would be a Set(3) Set result = Set(1, 2, 3).intersect(Set(3, 4));
- Parameters:
iterables
- the set of iterables the intersection should be calculated on- Returns:
- the product of the intersect operation
-
map
<U> Set<U> map(java.util.function.Function<T,U> mapper)
Description copied from interface:Streamable
Perform a mapping operation on the elements in the stream.This operation will loop over all elements in the stream and apply the
mapper
method. The mapped values will be returned in as a new stream of elements.- Specified by:
map
in interfaceCollection<T>
- Specified by:
map
in interfaceList<T>
- Specified by:
map
in interfaceStreamable<T>
- Specified by:
map
in interfaceTraversable<T>
- Type Parameters:
U
- the type of object expected as a result- Parameters:
mapper
- the mapping functionality- Returns:
- the mapped object
-
orElse
Set<T> orElse(java.lang.Iterable<? extends T> other)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise will return the providedother
.- Specified by:
orElse
in interfaceCollection<T>
- Specified by:
orElse
in interfaceList<T>
- Specified by:
orElse
in interfaceTraversable<T>
- Parameters:
other
- the alternative- Returns:
- this
Traversable
if non empty, orother
-
orElse
Set<T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.- Specified by:
orElse
in interfaceCollection<T>
- Specified by:
orElse
in interfaceList<T>
- Specified by:
orElse
in interfaceTraversable<T>
- Parameters:
supplier
- the supplier to generate the other- Returns:
- this (@code Traversable} if non empty, otherwise other
-
reject
default Set<T> reject(java.util.function.Predicate<T> predicate)
Description copied from interface:Traversable
Return a list that removes all elements that match thepredicate
provided.- Specified by:
reject
in interfaceCollection<T>
- Specified by:
reject
in interfaceList<T>
- Specified by:
reject
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to use- Returns:
- the elements that do not match the
predicate
-
remove
Set<T> remove(int index)
Description copied from interface:List
Removes an element from the list and returns a new instance of the list.
-
tail
Set<T> tail()
Description copied from interface:Collection
Build a new collection with all elements except for the head. If there is only one element present then an empty collection will be returned. If the operation is called on an empty collection anNoSuchElementException
will be thrown.
-
toJava
java.util.Set<T> toJava()
Transform this collection into one supported natively in Java.- Returns:
- the native java collection
-
union
Set<T> union(java.lang.Iterable<T> iterable)
Description copied from interface:List
Create a new sequence with all elements of this sequence combined with the elements of the provided iterable. The elements in this sequence will be added before the providediterable
in the returned sequence.Example:
// the example would be a List(1, 2, 3, 4) List<Integer> result = List(1, 2).union(List(3, 4));
-
-