public class PairList<S,T> extends java.lang.Object implements java.io.Serializable, java.lang.Iterable<Pair<S,T>>
Constructor and Description |
---|
PairList()
Constructor.
|
PairList(int initialCapacity)
Constructor.
|
PairList(java.util.Map<S,T> map)
Constructor to convert a map into a pair list.
|
PairList(PairList<S,T> other)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(S first,
T second)
Add the given pair to the list.
|
void |
addAll(PairList<S,T> other)
Adds all pairs from another list.
|
void |
addIfPresent(java.util.Optional<Pair<S,T>> pair)
Add the given pair to the list, if it is present.
|
boolean |
anyMatch(java.util.function.BiFunction<S,T,java.lang.Boolean> predicate)
Tests all the items against the predicate and returns true, if any of the
items matched the predicate.
|
void |
clear()
Clears this list.
|
static <S,T> PairList<S,T> |
concatenate(PairList<S,T> list1,
PairList<S,T> list2)
Returns a new
PairList with all elements from both lists in order. |
static <S,T> PairList<S,T> |
emptyPairList()
Returns an empty list (immutable).
|
protected void |
ensureSpace(int space)
Make sure there is space for at least the given amount of elements.
|
boolean |
equals(java.lang.Object obj) |
java.util.List<S> |
extractFirstList()
Creates a new list containing all first elements.
|
java.util.List<T> |
extractSecondList()
Creates a new list containing all second elements.
|
PairList<S,T> |
filter(java.util.function.BiFunction<S,T,java.lang.Boolean> filterPredicate)
Filters the pairlist by testing all items against the predicate and returning
a pairlist of those for which it returns true.
|
void |
forEach(java.util.function.BiConsumer<S,T> consumer)
For each element pair in this list, calls the given consumer with the first
and second value from the pair as the only arguments.
|
static <S,T> PairList<S,T> |
from(S key,
T value)
Creates a new pair list initialized with a single key/value pair.
|
static <S,T> PairList<S,T> |
fromPairs(Pair<S,T> pair,
Pair<S,T>... additionalPairs)
|
S |
getFirst(int i)
Returns the first element at given index.
|
UnmodifiableList<S> |
getFirstList()
Returns a list view of the first elements backed directly by the array.
|
T |
getSecond(int i)
Returns the second element at given index.
|
UnmodifiableList<T> |
getSecondList()
Returns a list view of the second elements backed directly by the array.
|
int |
hashCode() |
java.util.Optional<java.lang.Integer> |
indexOfFirst(S firstSearchObject)
Returns the index of the given object in the "first" list of this PairList or
Optional.empty if none of the first objects is equal to the given object.
|
void |
insert(int index,
S first,
T second)
Inserts the given element at the given index, shifting all other elements.
|
boolean |
isEmpty()
Returns whether the list is empty.
|
java.util.Iterator<Pair<S,T>> |
iterator() |
<S2,T2> PairList<S2,T2> |
map(java.util.function.BiFunction<S,T,S2> firstMapper,
java.util.function.BiFunction<S,T,T2> secondMapper)
|
<S2,T2> PairList<S2,T2> |
map(java.util.function.Function<S,S2> keyMapper,
java.util.function.Function<T,T2> valueMapper)
Maps a pair list to another one using separate mappers for keys and values.
|
<U,E extends java.lang.Exception> |
mapFirst(CollectionUtils.FunctionWithException<S,U,? extends E> mapper)
Returns a new
PairList , where the first elements are obtained by
applying the given mapper function to the first elements of this list. |
<U,E extends java.lang.Exception> |
mapSecond(CollectionUtils.FunctionWithException<T,U,? extends E> mapper)
Returns a new
PairList , where the second elements are obtained by
applying the given mapper function to the second elements of this list. |
void |
remove(int index)
Removes the element at given index.
|
void |
removeLast()
Removes the last element of the list.
|
PairList<S,T> |
reverse()
Returns a reversed copy of this list.
|
void |
setFirst(int i,
S value)
Sets the first element at given index.
|
void |
setSecond(int i,
T value)
Sets the first element at given index.
|
int |
size()
Returns the size of the list.
|
void |
sort(java.util.Comparator<Pair<S,T>> comparator)
Sorts the PairList according to the given comparator.
|
java.util.stream.Stream<Pair<S,T>> |
stream()
Returns a non-parallel stream of
Pair s from this list. |
void |
swapEntries(int i,
int j)
Swaps the entries located at indexes i and j.
|
void |
swapPairs()
Swaps the pairs of this list.
|
Pair<S,T>[] |
toArray()
Returns a newly allocated array containing all of the elements in this
PairList as an array of Pair
|
java.util.List<Pair<S,T>> |
toList()
Returns a newly allocated list containing all of the elements in this
PairList as a list of Pair
|
java.util.Map<S,T> |
toMap()
Converts this
PairList into a Map . |
java.util.Map<S,T> |
toMap(java.util.function.BinaryOperator<T> mergeFunction)
Converts this
PairList into a Map . |
static <S,T> org.conqat.lib.commons.collections.PairList.PairListCollector<S,T> |
toPairList()
Returns a collector for collecting a stream of pairs into a
PairList . |
java.lang.String |
toString() |
static <S,T> PairList<S,T> |
zip(java.util.List<S> firstValues,
java.util.List<T> secondValues)
Creates a
PairList that consists of pairs of values taken from the
two List s. |
public PairList()
public PairList(int initialCapacity)
public static <S,T> PairList<S,T> from(S key, T value)
@SafeVarargs public static <S,T> PairList<S,T> fromPairs(Pair<S,T> pair, Pair<S,T>... additionalPairs)
public boolean isEmpty()
public int size()
public void insert(int index, S first, T second)
protected void ensureSpace(int space)
public java.util.List<S> extractFirstList()
public UnmodifiableList<S> getFirstList()
public java.util.List<T> extractSecondList()
public UnmodifiableList<T> getSecondList()
public void swapPairs()
public void swapEntries(int i, int j)
public void clear()
public void removeLast()
public void remove(int index)
public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.util.stream.Stream<Pair<S,T>> stream()
Pair
s from this list.
NOTE: the underlying Spliterator
implementation does currently not
support splitting. Thus, trying to use the returned stream in parallel
execution will fail.
PairListSpliterator#trySplit()
public java.util.Map<S,T> toMap()
PairList
into a Map
.
If the first elements contain duplicates (according to
Object.equals(Object)
, an IllegalStateException
is thrown. If
the first elements may have duplicates, use toMap(BinaryOperator)
instead.public java.util.Map<S,T> toMap(java.util.function.BinaryOperator<T> mergeFunction)
PairList
into a Map
.
If the first elements contain duplicates (according to
Object.equals(Object)
, the respective second elements are merged
using the provided merging function..public static <S,T> org.conqat.lib.commons.collections.PairList.PairListCollector<S,T> toPairList()
PairList
.public static <S,T> PairList<S,T> zip(java.util.List<S> firstValues, java.util.List<T> secondValues)
PairList
that consists of pairs of values taken from the
two List
s. I.e. entry i in the resulting PairList
will
consists of the pair (a, b) where a is the entry at index i in the first
collection and b is the entry at index i of the second collection.
Both collections must be of the same size. The order of insertion into the
new PairList
is determined by the order imposed by the collections'
iterators.public void forEach(java.util.function.BiConsumer<S,T> consumer)
public <S2,T2> PairList<S2,T2> map(java.util.function.BiFunction<S,T,S2> firstMapper, java.util.function.BiFunction<S,T,T2> secondMapper)
public <U,E extends java.lang.Exception> PairList<U,T> mapFirst(CollectionUtils.FunctionWithException<S,U,? extends E> mapper) throws E extends java.lang.Exception
PairList
, where the first elements are obtained by
applying the given mapper function to the first elements of this list.E extends java.lang.Exception
public <U,E extends java.lang.Exception> PairList<S,U> mapSecond(CollectionUtils.FunctionWithException<T,U,? extends E> mapper) throws E extends java.lang.Exception
PairList
, where the second elements are obtained by
applying the given mapper function to the second elements of this list.E extends java.lang.Exception
public PairList<S,T> filter(java.util.function.BiFunction<S,T,java.lang.Boolean> filterPredicate)
public boolean anyMatch(java.util.function.BiFunction<S,T,java.lang.Boolean> predicate)
public Pair<S,T>[] toArray()
public java.util.List<Pair<S,T>> toList()
public void sort(java.util.Comparator<Pair<S,T>> comparator)
public static <S,T> PairList<S,T> concatenate(PairList<S,T> list1, PairList<S,T> list2)
PairList
with all elements from both lists in order.public static final <S,T> PairList<S,T> emptyPairList()
This example illustrates the type-safe way to obtain an empty Pairlist:
PairList<String, String> s = PairList.emptyPairList();
S
- key type of elements, if there were any, in the listT
- value type of elements, if there were any, in the listpublic void addIfPresent(java.util.Optional<Pair<S,T>> pair)
public <S2,T2> PairList<S2,T2> map(java.util.function.Function<S,S2> keyMapper, java.util.function.Function<T,T2> valueMapper)
public java.util.Optional<java.lang.Integer> indexOfFirst(S firstSearchObject)