How to convert Collection to Array in Java?

Ads
Ads

How to convert Collection to Array in Java?


Java Coder
How to convert Collection to Array in Java?
2 Answer(s)      a year and a month ago
Posted in : Java Beginners
Ads

Hello,

I have a collection object in data with data in it. How to convert Collection to Array in Java?

Thanks

View Answers

April 13, 2017 at 6:35 AM


Hi,

You can use the following code to create a collection:

  List<String> myList = new ArrayList<String>();
  list.add("This ");

Above code create a collection and then add an item to it.

Now to convert it into list myList.toArray() function can be used which converts Collection to Array in Java.

Here is the code for converting Collection to Array:

 String[] s1 = myList.toArray(new String[0]);

Check complete tutorial at Converting Collection to an Array.

Thanks



April 13, 2017 at 6:36 AM


Hi,

Check 100s of Java Util Examples.

Thanks


Ads