Easy way to transform Collection to Array?

Easy way to transform Collection to Array?

hello,

Is there easy way to get Array object from collection in Java? Easy way to transform Collection to Array?

Thanks

View Answers

April 13, 2017 at 9:21 AM

Hi,

Suppose you have a collection with the name myCol then you can use the following code:

 String[] myArray = myCol.toArray(new String[0]);

Above is using Java 7. But you can also do like this in Java 8:

String[] result = myCol.stream()
    .map(x -> new String(x))
    .toArray(size -> new String[size]);

Above code is using the stream in Java 8.

Thanks


April 13, 2017 at 9:23 AM

Hi,

We have huge collection of Java util package examples check all at Java Util Examples.

Thanks









Related Tutorials/Questions & Answers:
Easy way to transform Collection to Array?
Wt is easy way to learn UML
Advertisements
What is the best way to filter a Java Collection?
easy way to make a JCombobox look like a JTextField?
arrays
arrays
Arrays
Arrays
Collection
collection
multidimensional arrays
collection
Collection
How to Append Arrays in PHP
swap two integer arrays
swap two integer arrays
swap two integer arrays
java arrays
Arrays in java
Passing Arrays In Jsp Methods
collection overview and collection interface
Collection Types
java arrays
java arrays
Java with Arrays
java 2d arrays
Collection framework
Comparing Arrays
arrays
Arrays
Arrays
arrays
arrays
Arrays
arrays
arrays
arrays
Arrays
arrays
arrays
arrays
C Array copy example
Compare two Byte Arrays?
create arrays in JavaScript
How to create arrays in JavaScript?
Comparing arrays not working correctly?
Introduction to java arrays
Java Collection
Jaca Collection
Collection in java

Ads