Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Triple in Java?

Tags:

java

object

store

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?

like image 293
astro8891 Avatar asked Dec 17 '22 23:12

astro8891


1 Answers

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.

like image 134
UsamaAmjad Avatar answered Jan 04 '23 08:01

UsamaAmjad