I need to have a container for a couple of integers that I'm writing on a file, but it has to be of type int
instead of Integer
for the sake of minimum bytes taken (32 bits). I know that both Arraylists
and Vectors
won't take an int so is there a way to do that?
5 Answers
If you're writing for "minimum compression size," it doesn't matter how much memory your data takes up when the program's operating over it: only how much memory it takes when you write out the compressed file. It's perfectly fine to use boxed Integer
s to manipulate the data initially; you just have to convert when you're writing the file.
-
1You don't even have to manually convert. I can't imagine any such
int
-based code that would require any changes to work withInteger
instead.– Jason CNov 9, 2014 at 2:30
Use a int array, for storing primitive int type data.
Containers mean objects, that wrap the primitive data and provide additional functionality. Thus container instances would be larger than 32 bits for any wrapped int data.
There are collection libraries that support storing primitive values, but they are not part of the standard Java library.
Generally, you would only need one of these for specific performance or memory requirements. If you haven't even written your program, then you probably aren't in need of them.
You could also use an IntBuffer, but that won't give you a collection-like interface. Instead this is essentially a lightweight wrapper around a byte buffer (or primitive byte array) and is mostly used for reading and writing to and from files, network sockets, or interacting with native code.
-
-
@leventov It wasn't so much a recommendation as an example that this problem has been solved and for quite some time. If you've got better examples feel free to chime in.– b4handNov 9, 2014 at 17:33
if you'r not supposed to use a simple int[] you simply "cannot" do that in an easy way..
the int is a primitive not an object (as Integer)
public class IntContaner {
public int intOne;
public int intTwo;
}
(But, of course, anything you use will take more than 2x32 bits.)
byte[]
, or network buffer, or anything else -- it doesn't matter how much RAM it took, all that matters is that it only has 32 bits of information. AnInteger
is nothing more than a simple class with one field, anint value
.Integer
is not working for you, specifically or otherwise clarify your question. See also XY Problems.Integer
is a simple class with oneint
field virtually in the first sentence. You've wasted all of our time by being unclear and refusing to do a minimal amount of research on your own.Integer
instead ofint
I have ABC problems. I have tried Y to solve it to no avail. How can I approach this problem?" From your question, it appears you have tried nothing.