Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 11 September 2017

Java - Convert ArrayList Examples in Collection Framework

Java ArrayList Examples - Conversions

Java Convert ArrayList Examples in Collection Framework

Here we will see some java array list examples e.g How to covert LinkedList to ArrayList, How to convert ArrayList to String array, How to convert Vector to ArrayList, etc. This is article is also very important from core java interview point of view.

So let's start without any delay.


(1) Java Convert - LinkedList to ArrayList Example

This is the simple example of java LinkedList to ArrayList conversion.

import java.util.*;
class Simple1
{
public static void main(String args[])
{
//declaring LinkedList
LinkedList<String> ll = new LinkedList<String>();
ll.add("nice");//adding elements
ll.add("good");
ll.add("bad");
ll.add("better");
ll.add("nice");
ll.add("awesome");

//declaring ArrayList 
ArrayList<String> al = new ArrayList<String>(ll);

//Traversing elements by using Iterator interface
Iterator it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

Output: nice
             good
             bad
             better
             nice
             awesome


(2) Java - Convert ArrayList to String array

This is the simple example of ArrayList to String array conversion.

import java.util.*;
class Simple2
{
public static void main(String args[])
{
//declaring ArrayList Object
List<String> al = new ArrayList<String>();
al.add("india");//adding some elements to the array list
al.add("america");
al.add("england");
al.add("sri lanka");
al.add("india");

//creating String array
String[] str = new String[al.size()];
al.toArray(str);//using toArray() method for conversion

//traversing the elements one by one using for each loop
for(String s : str)
{
System.out.println(s);
}
}
}

Output: india
             america
             england
             sri lanka
             india


(3) Java - Convert Vector to ArrayList

This is another simple example of Vector class conversion to ArrayList class in java collection framework.

import java.util.*;
class Simple3
{
public static void main(String args[])
{
//declaring Vector object
Vector<String> v = new Vector<String>();
v.add("blue");//adding some values to the Vector
v.add("blue");
v.add("yellow");
v.add("green");
v.add("pink");
v.add("white");

//declaring ArrayList Object and pass Vector Object 
ArrayList<String> al = new ArrayList<String>(v);
//traversing elements
for(String s : al)
{
System.out.println(s);
}
}


Output: blue
             blue
             yellow
             green
             pink
             white


(4) Java - Convert Array to ArrayList

This is another simple example of Array to ArrayList conversion in java.

import java.util.*;
class Simple4
{
public static void main(String args[])
{
//declaring ArrayList
ArrayList<String> al = new ArrayList<String>();
//declaring and initializing String array
String str[] = {"priya", "suman", "simran", "siya"};
for(int i = 0; i<str.lenght; i++)
{
al.add(str[i]);
//adding array's elements to the arraylist
}
for(String s : al)
{
System.out.println(s);
}
}
}

Output: priya
             suman
             simran
             siya


(5) Java - Convert HashSet to ArrayList

In this example we will convert HashSet class into ArrayList in java.

import java.util.*;
class Simple5
{
public static void main(String args[])
{
//declaring HashSet

Set<String> hs = new HashSet<String>();
hs.add("pink");//adding some elements to the hash set
hs.add("red");
hs.add("blue");
hs.add("black");

//It will print hash set elements

System.out.println("HashSet Elements "+hs);

//declaring ArrayList
ArrayList<String> al = new ArrayList<String>(hs);

//It will array list elements in java
System.out.println("ArrayList Elements "+al);
}
}

Output: HashSet Elements [red, pink, blue, black]
             ArrayList Elements [red, pink, blue, black]

Above all the examples like linkedlist to arraylist, arraylist to string array, hashset to arraylist, vector to arraylist and array to arraylist are quite useful for any core java programming interview questions.

You can check this article to learn ArrayList class in detail.

Read More:

Java Collection Framework Interview Questions.
Difference Between ArrayList and Vector in Java.
Difference Between ArrayList and LinkedList in Java.
Difference Between HashSet and HashMap in Java.

Share:

2 comments:

  1. Great effort and good sharing, this will be helpful to everyone in the industry :
    )

    ReplyDelete
  2. You have share nice article , thanks for this. java vogue also shared some good tricks for doing same visit How to convert arraylist to array in java

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate