Forums Register Login

Is there any collection to store the premitive data type?

+Pie Number of slices to send: Send
Hi,
I just want to store a primitive data type in a collection say Vector,Hastable etc. But they store only Objects and byte is a premitive data type.
Is there any way out or I have to go for some Wrapper class for same only?
can you pl suggest?
regards,
arun
+Pie Number of slices to send: Send
In general, the solution is to use the Wrapper classes. Don't forget, though, that you can easily "extend" the existing collections and add a couple of methods specific to your types. Consider the following off-the-top-of-my-head, uncomiled code:
+Pie Number of slices to send: Send
Wrappers are indeed the preferred solution most of the time. It's also possible to use other collection-like classes which are optimized for specific primitive types. E.g. an array list of ints which uses an int[] array rather than an Object[], eliminating the need for wrappers and a lot of casting. However this is a bit risky, as unless you know what you're doing, it's easy to accidentally write a class which is slower &/or harder to use than the existing collections framework. It's probably not worthwhile trying to develop these yourself - there are already custom implementations floating around. I haven't used them, but the classes at trove4j.sourceforge.net look like they might be worth trying.
Remember though that by leaving the standard Java library classes, you're making it harder for other programmers to tell what you're doing or recognize bugs, since they're not nearly as familiar with how these nonstandard classes work. Thuse, you should probably stick to using wrappers unless you have good reasons for switching.
[ August 25, 2002: Message edited by: Jim Yingst ]
+Pie Number of slices to send: Send
Just another consideration...
I've also seen programmers use single dimesion arrays with one component to "wrap" a primitive value into an object. Remember that arrays are objects in Java.
+Pie Number of slices to send: Send
Is there any good reason to use an array instead of a wrapper, unless you are storing more than one type of value in the collection?
+Pie Number of slices to send: Send
Only that you can change the value in an array, where is the java.lang wrappers are immutable. However, you normally want to use immutable objects whenever possible, especially for stuff that gets passed around.
+Pie Number of slices to send: Send
Using J-Sprint, I performed a memory allocation analysis on these two programs:Test1 Results
Total number of bytes allocated: 1328912 (1.27MB)
Total number of bytes garbage collected: 0 (0.00MB)

Test2 Results
Total number of bytes allocated: 1569680 (1.50MB)
Total number of bytes garbage collected: 102960 (0.10MB)

If I get a chance I'll do a CPU profile test as well.
+Pie Number of slices to send: Send
I ran a few CPU consumption analysis tests using both J-Probe and JAMon. The results seem to be that either strategy consumes the CPU equivalently with the integer array strategy possibly winning by almost nothing according to J-Probe while the Integer strategy won by a similar (almost nonexistent) margin according to JAMon.
[ August 25, 2002: Message edited by: Dirk Schreckmann ]
+Pie Number of slices to send: Send
I ran some more memory consumption analyis tests, this time using the same code as previously posted, but adding another zero to the loop limit. This time, both strategies seemed to consume equivalent amounts of memory.
Test1 results
Total number of bytes allocated: 160129704 (152.71MB)
Total number of bytes garbage collected: 158916232 (151.55MB)
Test2 results
Total number of bytes allocated: 160130048 (152.71MB)
Total number of bytes garbage collected: 158916136 (151.55MB)

Now, I wonder what data access performance is like...
+Pie Number of slices to send: Send
Dirk,
Thanks for posting those stats. I had a similar situation last week where I had an array of integers that represented positions in a string and had to decide how to store them.
I chose to use the wrapper class and put together an ArrayList of Integers so that it had some flexibility in the future but was wondering if I'd cost myself anything in performance.
It seems like according to those stats. I might not have.
Thanks again!
Greg Ostravich
The name's Dirk. -ds
[ August 26, 2002: Message edited by: Dirk Schreckmann ]
+Pie Number of slices to send: Send
Collections will usually not perform as well as arrays. See the stats from my comparison tests:
http://www.javaranch.com/newsletter/June2002/newsletterjune2002.jsp#collections
+Pie Number of slices to send: Send
Greg,
I'm not sure that the testing I did is clear to you. I didn't test the performance of an array versus any collection, I tested the performance of a single element one dimensional array versus a wrapper class.
+Pie Number of slices to send: Send
OK - I did misunderstand.
You're just benchmarking the time to get data out of either the array element or the wrapper.
I still think I'll stick with using a wrapper class and a Collection instead of an int[].
It seems like there's some flexibility in case things change and it won't require tons of code changes that way if they do change.
Greg
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 958 times.
Similar Threads
string question .explanation pls
difference of int and INTEGER
can any one please help me to sort out this problems
OptionalDataException....???
Doubt in Mock Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 05:58:00.