Saturday, January 21, 2012

Collection to Array of Objects

Sometimes we need to convert collection of objects to array of objects.To do so java provide a easy to use method toArray which provides  bridge between collections and older APIs that expect arrays on input.

For example : suppose bookCollection  is a Collection and we can convert it to a new Object type array as :

Object bookObjectArray [] = bookCollection.toArray();

If bookCollection contains only specific type object such as String then we may want to convert the collection to String array . And we can do that as :

String bookStringArray [] = bookCollection.toArray(new String[0]) ; 

{suppose bookCollection contain only one String }


No comments:

Post a Comment