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

y2p
  • 4,791
  • 10
  • 40
  • 56
  • http://stackoverflow.com/questions/3725703/how-to-store-more-than-one-string-in-a-map/3725732#3725732 related – Bozho Oct 20 '10 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 :-) – TofuBeer Oct 20 '10 at 16:34

3 Answers3

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
}
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • @yO2gO, you should consider accepting some answer s to your questions so in the future people know which answer solved your problem. – jjnguy Oct 20 '10 at 16:04
  • Thanks. I am new to Stackoverflow. I accepted an answer this time and will do it henceforth – y2p Oct 23 '10 at 19:30
  • Hi @jjnguy How do I use 'listOfTuple.add()' in your above example? If I'll do - listOfTuple.add(Tuple);, this will not work. Thanks in advance. – Vibhav Chaddha Jan 08 '19 at 10:05
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>.

Yrlec
  • 3,401
  • 6
  • 39
  • 75
  • 1
    Ideally, of course, this class would have meaningful names for itself and its properties. – ColinD Oct 20 '10 at 15:55
  • 1
    Yes that's true. But that was unfortunately not specified in the question :) – Yrlec Oct 20 '10 at 15:56
  • Why do you have the getters and setters? It adds way too much unnecessary code. – jjnguy Oct 20 '10 at 15:57
  • @Yrlec: Yes, was pointing that out for the OP's benefit mostly! – ColinD Oct 20 '10 at 15:59
  • 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. – Yrlec Oct 20 '10 at 16:00
  • @jjnguy: It wouldn't surprise me if these 3 values should have been grouped in a class to begin with, in which case it would likely be useful elsewhere and worth it to make it a proper class. If it's just a throwaway only for use with this particular list, then maybe I wouldn't bother. – ColinD Oct 20 '10 at 16:01
  • @Colin, agreed. I just figured this to be a throw away class, thus, I designed my implementation to be as light as possible. – jjnguy Oct 20 '10 at 16:03
  • Past experience leads me to say don't do this for real - it becomes a royal pain usually. – TofuBeer Oct 20 '10 at 16:33
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.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • I don't recommended this non generic solution. – jjnguy Oct 20 '10 at 15:56
  • 1
    Neither do I! :) Just presenting other alternatives. – Daniel DiPaolo Oct 20 '10 at 15:57
  • 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` should be avoided when possible. – ColinD Oct 20 '10 at 15:58