Patrick Patrick - 25 days ago 9
Java Question

Java: how to store data triple in a list?

What's the best way in java to store data triple in a list ?

[a, b, c]
[a, b, c]
...


I usually use HashMap for couples of data key + value.. should I go for HashMap + Arraylist ? or ArrayList + ArrayList ?

thanks

Answer
public class Triplet<T, U, V> {

    private final T first;
    private final U second;
    private final V third;

    public Triplet(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 to instantiate the list:

List<Triplet<String, Integer, Integer>> = new ArrayList<>();
Comments