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
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:
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 ]
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.
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 ]
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...
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, 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.
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
Greg Ostravich - SCPJ2
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth