2

In my Java program, I want to store a list of primitive values. I could do something like this:

int x = 0;
double timestamp = 123.1d;
List<Object> data = new ArrayList<Object>();
data.add(x);
data.add(timestamp);

But then the problem is that I do not know exactly what kind of objects i store in the list. So is there any better way to do that?

Fischer Ludrian
  • 629
  • 1
  • 9
  • 23
  • Why not using two lists ? – Konstantin Yovkov Apr 23 '14 at 11:55
  • Because there can be an arbitrary amount of primitive values. – Fischer Ludrian Apr 23 '14 at 11:55
  • To be honest, I gave up and wrote specialized versions of an ArrayList for all primitive types. So I got `DoubleArray` and you could just add ints to it. – tilpner Apr 23 '14 at 11:57
  • you can use wrapper class instead primitive datatype like Interger instead of int. If you want to use wrapper then i have solution for your problem. – subhash lamba Apr 23 '14 at 11:57
  • @subhashlamba: he's *already* using wrapper classes. The compiler auto-boxes the primitive values automatically. – JB Nizet Apr 23 '14 at 11:58
  • This is one of those cases where upcasting is not a good idea. Safer and robust solution - Do as Kocko says seperate lists. maybe a list of lists where you will be knowing that the first list is of ints, second is of doubles etc. – TheLostMind Apr 23 '14 at 12:02
  • @WhoAmI Separate lists are possible given he doesn't rely on order of elements. If he does then separate lists would be very complicated. Check my answer http://stackoverflow.com/a/23243862/2709026 – tzima Apr 23 '14 at 13:00
  • @close voters: Imho, this i not a duplicate! I want to store different primitive data types in a list and not only ints. – Fischer Ludrian Apr 23 '14 at 14:14
  • I also do not think this is a duplicate, it is about having different types in the List, not about just using a wrapper class – LionC Apr 23 '14 at 14:20

3 Answers3

7

Well, Double, Integer, Long all belong to the Number-class. So a

List<Number>

would probably fit. It is exactly what it is - a List of numbers of unspecified subtype. Because of autoboxing you should be able to just add the primitives, but the better practice would be to use the Wrapper-classes.

The Number-class offers methods to get the different representations of the Number, for example doubleValue(). So you could convert all the values in the List<Number> to (as an example) Doubles by using this method. For more reference see the Oracle documentation for Number.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • 4
    yes.. But you'll have to downcast when you get the value back. And if the numbers(of ints, longs, shorts, doubles etc) are arbitrary, you could end up casting a double into a short – TheLostMind Apr 23 '14 at 11:58
  • 1
    for that case Object would be best which can accomodate anything and later downcasting based on the type – Susheel Singh Apr 23 '14 at 11:59
  • @WhoAmI well if you want to store different subclasses of a class in one data structure of one type this will be unavoidable. – LionC Apr 23 '14 at 11:59
  • @WhoAmI The Number class offers you methods to get the Number in the idfferent formats so you do not loose anything by just using these to convert to the type you need. – LionC Apr 23 '14 at 12:01
  • @susheel No. Why should we use Object if we know that it are Numbers? Number even offers us helper methods to deal with exactly this problem, because Number exists for this purpose. A List that can only contain Numbers is really bad design. – LionC Apr 23 '14 at 12:10
  • if we know that its all numbers then its not a problem. he was talking about mixed content i felt – Susheel Singh Apr 23 '14 at 12:11
2

You could use List<Object> and add any kind of object to it.While retrieving the Object back from List<Object> you can get class of object by list.get(0).getClass() or you could check for list.get(i) instacne of Double.

er_suthar
  • 319
  • 1
  • 8
1

use wrapper class and use instance of operator to check datatype of value. like

Integer x = 0;
Double timestamp = 123.1d;
List<Object> data = new ArrayList<Object>();
data.add(x);
data.add(timestamp);
if(data.get(0) instance of Integer)
{
...
}
else if(data.get(0) instance of Double )
{
..
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
subhash lamba
  • 216
  • 1
  • 14