How do I Convert Collection to ArrayList?

Category: java.util, viewed: 23361 time(s).
package org.kodejava.example.java.util;

import java.util.ArrayList;
import java.util.LinkedList;

public class CollectionToArrayList 
{    
    public static void main(String[] args)
    {
        // We create LinkedList collection type at put some values
        // in it. Here we put A, B, C and D letter into it.
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.push("A");
        linkedList.push("B");
        linkedList.push("C");
        linkedList.push("D");
        
        // Let say you want to convert it to other type of collection,
        // for instance here we convert it into ArrayList. To do it
        // we can pass the collection created above as a parameter to
        // ArrayList constructor.
        ArrayList<String> arrayList = new ArrayList<String>(linkedList);
        
        // Now we have converted the collection into ArrayList and
        // printed what is inside.
        for (String s : arrayList)
        {
            System.out.println("s = " + s);            
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats