Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Convert Collection to ArrayList

This Java convert Collection to ArrayList example shows how to convert any collection to ArrayList.
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3.  
  4. //Java Convert Collection to ArrayList
  5.  
  6. public class ConverCollectionToArrayList {
  7.    
  8.     public static void main(String args[]){
  9.        
  10.         //Create new HashSet
  11.         HashSet<String> hashSetColors = new HashSet<String>();
  12.         hashSetColors.add("White");
  13.         hashSetColors.add("Black");
  14.         hashSetColors.add("Blue");
  15.        
  16.         /*
  17.          * To convert any Collection to ArrayList use,
  18.          * ArrayList(Collection collection) constructor of Java
  19.          * ArrayList class.
  20.          */
  21.         ArrayList<String> aListColors = new ArrayList<String>(hashSetColors);
  22.        
  23.         //print values of an ArrayList created from Collection
  24.         for(String color: aListColors){
  25.             System.out.println(color);
  26.         }
  27.     }
  28. }
  29.  
  30. /*
  31. Output of above given Java Collection to ArrayList example would be
  32. Black
  33. Blue
  34. White
  35. */
Your rating: None Average: 3 (2 votes)
Share this

Post new comment