If you have an instance of a Collection, say something like:
Collection<String> addresses = new ArrayList<String>();
Which were to then be populated with a bunch of values, which is the ‘best’ way, if any, to make use of the toArray() method without requiring a type cast?
String[] addressesArray = addresses.toArray(new String[] {}); String[] addressesArray = addresses.toArray(new String[0]); String[] addressesArray = addresses.toArray(new String[addresses.size()]); String[] addressesArray = addresses.toArray(new String[addresses.size() + 5]);
Is there any semantic difference between the first two? Is the third most efficient? Is the fourth less efficient than the third?
According to a findbugs report, this is the most efficient:
I believe them 😉