Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to store data triple in a list?

Tags:

java

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

like image 200
aneuryzm Avatar asked May 15 '11 19:05

aneuryzm


People also ask

Is there a triple in Java?

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".

How many elements can you store in a list?

1) Yes, list can store 100000+ elements.

How does list store data in Java?

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.


1 Answers

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<>(); 
like image 144
Bala R Avatar answered Sep 20 '22 06:09

Bala R