Skip to main content

The Java 8 Lambda way: ArrayList to an Array
15 July 2016

In an earlier post this year, 'Convert an ArrayList to an Array in Java', I commented on a way to convert an ArrayList to an Array in Java. Here is the example code from the earlier post.

List<String> results = new ArrayList<>();
...
results.toArray(new String[results.size()]);

This earlier way works fine, however it's old school. Here is the Java 8 Lambda way to convert an ArrayList to an Array:

Long[] longArray = longArrayList.stream()
    .map(Long::new)
    .toArray(Long[]::new);

This example works by mapping a new object for each item in the ArrayList's stream to the new stream, and then collecting that stream as an array.

Mailing List


Responsive Media

With the ResponsiveMedia plugin for Joomla it is easy to add 3rd party content from YouTube, Vimeo, and Instagram right in to any Joomla! article.

ResponsiveMedia