File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes casting into primitive wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "casting into primitive wrapper class" Watch "casting into primitive wrapper class" New topic

casting into primitive wrapper class

Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
hi there,
playing with the Collection Framework, I was wondering about the following issue:
What is the most efficient way (or: _any_ way but a loop) to cast a array of primitives
int myArray[] = {1,2,3,4,5}
into a array of Integer wrapper objects?
My goal was to be able to handle myArray[] as a Collection.
Arrays.asList(Object[] a)
looked like a appropriate method to me. But, as already quoted, I had to cast it manually to a help-array
Integer myHelpArray[]
which worked out, but doesn't look too efficient to me ;-)
thanks for any help,
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Jan!
We beginners aren't too interested in alleged efficiencies, especially as premature optimization is the root of all evil [Knuth]. We're more concerned with clarity of code.
Moving this to the Performance forum...
[ February 03, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Hey Dirk,
thanks for answering so fast!
As english is not my mother tongue, it seems to me that I put my question a bit too much into the "hey, how can I get the last edge of performance out of my code?"-corner.
Considering myself as a beginner with java, let me try to put my intention behind the question in other words:
I have an array of primitves. I'd love to transform it into a Collection, to do all the fancy collection-stuff with it.
The only method I found which vaguely does what I want is "Arrays.asList(Object[] a)".
My first question: Is this the only way? Is this okay or does it look too complicated or so? Are there other (better?) methods?
When I'm using this method, I need an array of Objects, not of primitives. The only way I found to get this is to cast element per element from "int" to "Integer".
My second question: Is looping through the whole array the only way? -> If I do so I could also fill a Collection with my array's elements...
From my practise in other languages and my impression of Java I feel that there might be a simple one-liner of code, recieving my array of primitives and returning a object of a class that implements Collection.
If you say "no, the way is definitely okay", I'll be more then happy. Otherwise, just let me know how you would have solved this "problem"
Greetings from Berlin, Germany
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13027
No there is no one-liner to change an array of primitives to an array of wrapper class objects.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
The best way would be ( in pseudo code ):
Create List
For Each Element In Primitive Array
Convert Element To Wrapped Element
Add Wrapped Element To List
does that help ?
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
thanks for answering.
So I summarize:
- There is no one-step way to get a array of primitives into a collection
- The way I tried to do it was okay

Thanks again !
Anjaiah Setty

Joined: Jul 12, 2005
Posts: 3
Hello There,

I need the opposite -- a "long []" from a "Long []".

A method needs to return a long [] from a DB/SQL query. Inside the method, we create an ArrayList<Long>. Now what is an easy way to convert this ArrayList to a "long []" ?

I could NOT find anything in jakarta commons/ClassUtils.

Thanks for any pointers.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

There's no better way than to do the obvious:

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: casting into primitive wrapper class
It's not a secret anymore!