11

I want to do something like this

List<Integer, String, String>

I want to be able to iteratively retrieve either of these three parameters. How can I go about it? Thanks

2
  • stackoverflow.com/questions/3725703/… related Commented Oct 20, 2010 at 15:58
  • Why do you want to do this? Generally when I see this it is a design problem (speaking from my own mistakes as well as others :-) Commented Oct 20, 2010 at 16:34

4 Answers 4

7

What you need is a Tuple class:

public class Tuple<E, F, G> {
    public E First;
    public F Second;
    public G Third;    
}

Then you can iterate over the list of the tuple, and look at each entry in the tuple.

List<Tuple<Integer, String, String> listOfTuple;
for (Tuple<Integer, String, String> tpl: listOfTuple){
    // process each tuple
    tpl.First ... etc
}
Sign up to request clarification or add additional context in comments.

3 Comments

@yO2gO, you should consider accepting some answer s to your questions so in the future people know which answer solved your problem.
Thanks. I am new to Stackoverflow. I accepted an answer this time and will do it henceforth
Hi @jjnguy How do I use 'listOfTuple.add()' in your above example? If I'll do - listOfTuple.add(Tuple<someInteger, someString, someString2>);, this will not work. Thanks in advance.
3

You can create a wrapper class which holds these three variables and then store that wrapper-object in the list.

For instance

    public class ListWrapperClass {
    private String firstStringValue;
    private String secondStringValue;
    private Integer integerValue;

    public String getFirstStringValue() {
        return firstStringValue;
    }

    public void setFirstStringValue(String firstStringValue) {
        this.firstStringValue = firstStringValue;
    }

    public String getSecondStringValue() {
        return secondStringValue;
    }

    public void setSecondStringValue(String secondStringValue) {
        this.secondStringValue = secondStringValue;
    }

    public Integer getIntegerValue() {
        return integerValue;
    }

    public void setIntegerValue(Integer integerValue) {
        this.integerValue = integerValue;
    }
}

and then use List<ListWrapperClass>.

8 Comments

Ideally, of course, this class would have meaningful names for itself and its properties.
Yes that's true. But that was unfortunately not specified in the question :)
Why do you have the getters and setters? It adds way too much unnecessary code.
@Yrlec: Yes, was pointing that out for the OP's benefit mostly!
I prefer to use getters and setters to make it easier to add functionality later on. If I let external classes access the fields instead I make it a lot more difficult to add, for instance, validation before a field gets set.
|
1

You can use a List<Object> and then cast whatever you retrieve based on the index, but you may just consider creating a class that holds these three things.

3 Comments

I don't recommended this non generic solution.
Neither do I! :) Just presenting other alternatives.
This doesn't seem like the right choice because the 3 values could not all share the same index and so are not related to each other as they should be. Plus, the lack of type safety with List<Object> should be avoided when possible.
0

Another alternative is to use a Tuple from a dependency in Maven central repo

<dependency>
    <groupId>org.javatuples</groupId>
    <artifactId>javatuples</artifactId>
    <version>1.2</version>
</dependency>

You then create an object with two or more types like this:

Pair<String, Integer> person = new Pair<>("John", 50);
Triplet<Integer, String, String> person1 = new Triplet<>("John", 50, "single")

There are 10 types of tuples, each for a corresponding number of items:

Unit (one element)
Pair (two elements)
Triplet (three elements)
Quartet (four elements)
Quintet (five elements)
Sextet (six elements)
Septet (seven elements)
Octet (eight elements)
Ennead (nine elements)
Decade (ten elements)

And then you can just use it in a list like any other type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.