Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.util.ArrayList;
- import java.util.HashSet;
- //Java Convert Collection to ArrayList
- public class ConverCollectionToArrayList {
- public static void main(String args[]){
- //Create new HashSet
- HashSet<String> hashSetColors = new HashSet<String>();
- hashSetColors.add("White");
- hashSetColors.add("Black");
- hashSetColors.add("Blue");
- /*
- * To convert any Collection to ArrayList use,
- * ArrayList(Collection collection) constructor of Java
- * ArrayList class.
- */
- ArrayList<String> aListColors = new ArrayList<String>(hashSetColors);
- //print values of an ArrayList created from Collection
- for(String color: aListColors){
- System.out.println(color);
- }
- }
- }
- /*
- Output of above given Java Collection to ArrayList example would be
- Black
- Blue
- White
- */

Expertise remain remarkably
Post new comment