tags:

views:

148

answers:

4

Can I contain two different types in a collection? For example, can I have List< String U Integer > ?

+4  A: 

Short answer ? No. You can (of course) have a List of Objects, but then you can put anything in it, not just String or Integer objects.

You could create a list of container objects, and that container object would contain either an Integer or String (perhaps via generics). A little more hassle.

public class Contained<T> {
   T getContained();
}

and implement Contained<Integer> and Contained<String>.

Of course, the real question is why you want to do this ? I would expect a collection to contain objects of the same type, and then I can iterate through and perform actions on these objects without worrying what they are. Perhaps your object hierarchy needs further thought ?

Brian Agnew
+2  A: 

Nope. You have a couple of alternatives, though:

  • You can use a List < Object > and stash whatever you like; or

  • You can use a List < Class-with-2-members > and put your data in one of those class members.

EDIT: Example.

class UnionHolder {
  public String stringValue;
  public int intValue;
}

List < UnionHolder > myList 
...

Of course you'll need a bit of additional code to figure out which kind of data to pull out of the UnionHolder object you just got out of your list. One possibility would be to have a 3rd member which has different values depending on which it is, or you could, say, have a member function like

public boolean isItAString() { return (this.stringValue != null }
Carl Smotricz
Could you please explain the second option?
kunjaan
Sure. Coming up.
Carl Smotricz
+1  A: 

No. Think about it this way: with generics, the whole idea is to provide type safety. That would not be possible if you could put Objects of different types into it.

You can use the non-generic java.util.List for your purpose.

If you want to ensure that only String or Integer objects enter the list, you could create your own List implementation like so:

public class MySpecialList {
   private List list= new LinkedList();
   ...
   public void add(final String string) {
       list.add(string);
   }

   public void add(final Integer integer) {
       list.add(integer);
   }
   ...
   // add rest of List style methods
}

Drawback: you loose the List interface clarity...

Gerd Klima
+1  A: 

In addition to the nice answers already provided ...


Possibly, you have the two data types in your algorithm. But you may not have to put them in the same list...

Creating two typed lists could be the clearer for your algorithm, you would still keep the "type-safeness" and carry all your data. Two code samples follow, the second grouping the two lists in a MyData object.

public class Algorithm1 {

  public void process(List<String> strings, List<Integer> integers) {
    ...
  }

}

--------------------------------------

public class DataPair {

  public List<String> strings;
  public List<Integer> integers;

}

public class Algorithm2 {

 public void process(DataPair dataPair) {
   ...
 }

}
KLE