Converting a Collection to an Array

// converting an ArrayList into an array of objects
											Object[] objects = aArrayList.toArray();
											// converting a HashMap into an array of objects
											Object[] mapObjects = aHashMap.entrySet().toArray();

As you can see in this phrase, it is a relatively simple task in Java to convert a collection, such as an ArrayList or HashMap, into a regular array of objects. The ArrayList has a toArray() method that returns an array of objects. Converting a HashMap to an array is slightly different. First, we must get the values stored in the HashMap as an array, using the entrySet() method. The entrySet() method returns us the data values as a Java Set. Once we have the Set object, we can call the toArray() method to get an array ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.