September 2019
Intermediate to advanced
816 pages
18h 47m
English
In order to convert a collection into an array, we can rely on the Collection.toArray() method. Without arguments, this method will convert the given collection into an Object[], as in the following example:
List<String> names = Arrays.asList("ana", "mario", "vio");Object[] namesArrayAsObjects = names.toArray();
Obviously, this is not entirely useful since we are expecting a String[] instead of Object[]. This can be accomplished via Collection.toArray(T[] a) as follows:
String[] namesArraysAsStrings = names.toArray(new String[names.size()]);String[] namesArraysAsStrings = names.toArray(new String[0]);
From these two solutions, the second one is preferable since we avoid computing the collection ...