2

How do you store primitive numbers in Java lists? When I try to do

List<double> list = new ArrayList<>();

The compiler tells me that double isn't an object. It seems that the Collections weren't made for primitives. My clumsy workaround is as follows:

List<Double> list = new ArrayList<>();

But this is slower and uses more memory. Is there a better way to do this?

Simon Kuang
  • 3,870
  • 4
  • 27
  • 53
  • That's the right way. – Christian Tapia Mar 03 '14 at 16:26
  • No, it's the only way to do it. – Bart Mar 03 '14 at 16:26
  • Use a `double[]`? And if you want dynamically increasing array, then you won't have any other option in standard Java. BTW, why do you think it's slow. Do you have some benchmark to prove that point? – Rohit Jain Mar 03 '14 at 16:26
  • See http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types – Arnaud Denoyelle Mar 03 '14 at 16:26
  • Java generics and primitives don't go together. "But this is slower and uses more memory. Is there a better way to do this?" Yes, you can use a primitive array instead. – Bhesh Gurung Mar 03 '14 at 16:27
  • So many short, low effort answers. Answerers, please elaborate and discuss reasons `List` doesn't work and alternatives to it. For example, what are the performance implications of `double[]` vs. `List`? When does the tradeoff matter? – John Kugelman Mar 03 '14 at 16:36

5 Answers5

2

You cannot use primitives directly in a collection in Java. This is a deliberate design-decision in Java (though some think it is a mistake). Generics in Java were bolted on after the fact, which is why their application is not uniform across the language.

Containers basically want Object types and primitive types aren't derived from Object (i.e., Object is not their superclass unlike every other object in Java). From a code-writing point of view, it certainly looks like there are no wrappers:

list.add(6); //list is of type List<Integer>
int num = list.get(i); 

This is because the boxing and unboxing is done automatically for you. At the bytecode level, what you actually have is:

list.add(new Integer(6));
int num = ((Integer) list.get(i)).intValue();

So the second option you have is the right way in Java.

If you want a less memory-hungry option, you can opt for a straight double[]. However, this means that you are going to have to write code to manage add, retrieve, update, and delete operations. If that is too much extra work, you can try using Apache Commons Primitives.

Also, I suspect you meant List<Double> list = new ArrayList<>(); (or some other implementation), right? This is because what you have won't compile.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

No, there's no better way. Instances of Collection can only hold onto objects, not primitives. So your workaround is the way to go.

Unless you are coding a time critical component, wondering about memory usage and/or performance in this case might indicate you are victime of premature optimization.

Laf
  • 7,965
  • 4
  • 37
  • 52
0

You can't use collections with primitives. You can only store objects in collections. Those objects will be unboxed automatically.

This is only way what it can be done.

You can get more info from this link: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
  • @Downvoter: Can you explain the reason for downvoting? – Bosko Mijin Mar 03 '14 at 16:40
  • Your answer only addresses part of the question. You don't address performance, and why autoboxing is or is not an acceptable performance hit. In my opinion, an "easy" question like this deserves a comprehensive answer. – John Kugelman Mar 03 '14 at 17:58
0

There is no better way. At least Java provides automatic conversions back and forwards between e.g. double and Double to make this less painful ('autoboxing' / 'autounboxing' as it is known)

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15
-2

You can use a collection. double is not an Object but Double it is.

Luixv
  • 8,590
  • 21
  • 84
  • 121