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 containing the values that were stored in the HashMap.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net