Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert collection to primitive array

 
Niclas Åstrand
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

Is there a way, other than writing your own method, for converting a collection of say Long (List<Long>) to an array of long?

Niclas
 
Roel De Nijs
Sheriff
Posts: 10040
127
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niclas,

This is more a Java in General or Beginning Java question. I know it is for the SCJD, because I had exactly the same question as you and you need it I guess for the find-method I googled around, found nothing appropriate and just wrote 4 lines of really impressive code to get the job done

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So let's slide this over to the Beginning Java forum!
 
Niclas Åstrand
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting in the wrong forum.

Thanks Roel.

Niclas
 
senthil kumar. J
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use toArrary() method provided by the List and pass list to an array.

 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
senthil kumar. J wrote:Why don't you use toArrary() method provided by the List and pass list to an array.


Please note that this does not compile:

The Arrays.asList call is OK, the primitive ints will be autoboxed into Integer.
However, the signature of toArray is:
<T> T[] toArray(T[] a)

Here T must stand for a reference type, not a primitive.

Unfortunately, T is not related to E (Java does not allow type parameters to be bounded using super, so <T super E> T[] toArray(T[] a) is illegal), which means you can compile the following (obviously invalid) code:


 
Niclas Åstrand
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your excellent clarification Istvan!
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niclas Åstrand wrote:Is there a way, other than writing your own method, for converting a collection of say Long (List<Long>) to an array of long?

There's no other way in the standard classes included in the JDK, as far as I know. Of course third-party libraries may do it for you. The only one I know that has this is Guava - see Ints.toArray() for example. Wouldn't surprise me if there are others. Then again, this is a really simple method to write for yourself, so it's not worth putting a lot of effort into searching for other implementations.
 
Roel De Nijs
Sheriff
Posts: 10040
127
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:There's no other way in the standard classes included in the JDK, as far as I know. Of course third-party libraries may do it for you.

He asks this question with regard to SCJD certification, so use of 3rd party libraries are not allowed, only the JDK. So he will to code this on his own If Guava is open-source you can get the sources and have a look at the Ints.toArray() method for a bit of inspiration
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic