I've come across code that looks like the following:
public List<Triple<String, String, Instant>> methodName() {
// Do something
}
What is the Triple
, how should It be used?
Triple is useful when you want to save 3 values at a time and you can pass different data types. If you just want to learn then below is an example of its usage but if you want to use in code then I would suggest you to use Objects
instead.
public class Triple<T, U, V> {
private final T first;
private final U second;
private final V third;
public Triple(T first, U second, V third) {
this.first = first;
this.second = second;
this.third = third;
}
public T getFirst() { return first; }
public U getSecond() { return second; }
public V getThird() { return third; }
}
And this is how you can instantiate it:
List<Triple<String, Integer, Integer>> = new ArrayList<>();
EDIT
As discussed in comments, please note that it belongs to org.apache.commons.lang3.tuple
It is not a built-in class in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With