Package com.yetnt.utils.tuple
Record Class Triple<T>
java.lang.Object
java.lang.Record
com.yetnt.utils.tuple.Triple<T>
- Type Parameters:
T- The type of the elements in the triple.- Record Components:
v1- The first valuev2- The second valuev3- The third value
An immutable record representing a tuple of three elements of the same type.
From J3Engine
- Author:
- Lehlogonolo Poole
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.static <T,V> void forEachPair(Triple<T> t1, Triple<V> t2, BiConsumer<T, V> forEachConsumer) Applies the given consumer to each positional pair of the two input triples.static <T> Triple<T> Creates a newTriplerecord from a given listfinal inthashCode()Returns a hash code value for this object.<T2> Triple<T2> Applies a given mapping function to each element of this Triple, returning a new Triple with the transformed elements.static <T,U, V> Triple <V> mapPair(Triple<T> t1, Triple<U> t2, BiFunction<T, U, V> mapper) Maps each positional pair of the two input triples back into the same position of a new triple.<K> KmapTo(TriFunction<T, T, T, K> map) Applies the given function to the triple to return a single value.stream()Creates a stream of the 3 elementsConverts this Triple into anArrayListcontaining its three elements.final StringtoString()Returns a string representation of this record class.v1()Returns the value of thev1record component.v2()Returns the value of thev2record component.v3()Returns the value of thev3record component.
-
Field Details
-
Constructor Details
-
Method Details
-
from
Creates a newTriplerecord from a given list- Type Parameters:
T- The type held by the list- Parameters:
list- The list. Should have at least 3 elements- Returns:
- A new Triple with the 3 elements
- Throws:
IllegalArgumentException- If the list is empty or the list has less than 3 elements.
-
mapTo
Applies the given function to the triple to return a single value.- Type Parameters:
K- The type of the single value to return- Parameters:
map- The mapper function- Returns:
- A single value
-
toArrayList
Converts this Triple into anArrayListcontaining its three elements. The order of elements in the list will be v1, v2, v3.- Returns:
- An
ArrayListcontaining the elements of this Triple.
-
map
Applies a given mapping function to each element of this Triple, returning a new Triple with the transformed elements.- Type Parameters:
T2- The type of the elements in the new Triple after mapping.- Parameters:
mapper- The function to apply to each element.- Returns:
- A new
Triplecontaining the results of applying the mapper function to each element.
-
stream
Creates a stream of the 3 elementsThis is functionally identical to calling
Triple<String> triple = new Triple("hello", "world", "!"); Stream<String> str = Stream.of(triple.v1(), triple.v2(), triple.v3());- Returns:
- A stream of the input stuff
-
forEachPair
Applies the given consumer to each positional pair of the two input triples.This is if, you were to take
v1of each Triple, and consume them with aBiConsumer, applied to each argument in one call.- Type Parameters:
T- The type the first triple holdsV- The type the second triple holds- Parameters:
t1- The first triplet2- The second tripleforEachConsumer- The consumer to apply to each positional argument
-
mapPair
Maps each positional pair of the two input triples back into the same position of a new triple.This is if you were to take
v1of each Triple, apply the map then save that asv1of a new triple, applied to each argument in one call.Triple<Integer> a = new Triple<>(1, 2, 3); Triple<Integer> b = new Triple<>(10, 20, 30); Triple<Integer> result = TripleUtils.mapPair(a, b, Integer::sum); // (11, 22, 33)- Type Parameters:
T- The type held by the first tripleU- The type held by the second tripleV- The type held by the new returned triple- Parameters:
t1- The first triplet2- The second triplemapper- The map to apply to each positional element of the triple- Returns:
- A new triple
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
v1
Returns the value of thev1record component.- Returns:
- the value of the
v1record component
-
v2
Returns the value of thev2record component.- Returns:
- the value of the
v2record component
-
v3
Returns the value of thev3record component.- Returns:
- the value of the
v3record component
-