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
That Triple class is the Java way of providing you something like that. Like a Pair, but one more entry. At its core, a fixed length tuple allows you to "loosely couple" multiple values of different types based on some sort of "ordering".
1) Yes, list can store 100000+ elements.
As you already noticed, the List interface can not store data. But the ArrayList can and stores the data in an array. A LinkedList stores that information as linked list. But you can use both of them interchangeable when you only use the List interface.
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<>();
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