my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Is there any collection to store the premitive data type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there any collection to store the premitive data type?" Watch "Is there any collection to store the premitive data type?" New topic
Author

Is there any collection to store the premitive data type?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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...
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
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 ]

Greg Ostravich - SCPJ2
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there any collection to store the premitive data type?
 
Similar Threads
string question .explanation pls
can any one please help me to sort out this problems
difference of int and INTEGER
OptionalDataException....???
Doubt in Mock Question