• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there any collection to store the premitive data type?

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic