O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required