Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an object to pair of pair list in Java?

Tags:

java

android

the list type:

List<Pair<Integer, Pair<Integer, Integer>>> listPairOfPair = null;

I tried these but it didn't work:

listPairOfPair.add(new Pair<Integer, Pair<Integer, Integer>>(1, (2,3));

listPairOfPair.add(new Pair<Integer, Pair<Integer, Integer>>(1, Pair.create(2,3)));
like image 530
Maor Cohen Avatar asked Oct 27 '25 14:10

Maor Cohen


1 Answers

Lets go for the "simply"; the "level by level" thing:

Pair<Integer, Integer> onePair = new Pair<>(1, 2); // assuming that you are using that android class that has this ctor

... creates a single Pair.

Pair<Integer, Pair<Integer, Integer>> pairOfPairs = new Pair<>(3, onePair);

... creates a Pair of Integer and the previously created pair.

List<Pair<Integer, Pair<Integer, Integer>>> listOfPairedPairs = new ArrayList<>();
listOfPairedPairs.add(pairOfPairs);

... creates the list and adds one element. That can be simplified a little bit, with:

listdOfPairedPairs = Arrays.asList(pairOfPairs, someOtherPair, ...);

And of course, you can write methods such as:

public Pair<Integer, Pair<Integer, Integer>> of(Integer i1, Integer i2, Integer i3) {
  ... check that uses such code and returns such a paired pair

And use that like:

listdOfPairedPairs = Arrays.asList(of(1,2,3) , of(4,5,6));

But of course, if you are really using that android.util Pair implementation; then you better follow the advise from Nicolas' and use Pair.create() !

like image 168
GhostCat Avatar answered Oct 30 '25 05:10

GhostCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!