Anirudh Bhatnagar

About Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.

Java Array to List Example

In this example I will show you how to convert an array into a java.util.List.
We will use the java.util.Arrays class to convert an Object Array into a List of Objects.

Lets see the example:

1) Object Array to List.

A List can only store objects and not the primitives. So, lets how we can convert an array of objects into a list.

 

ObjectArrayToList

package com.javacodegeeks.example;

import java.util.Arrays;
import java.util.List;

/**
 * Created by anirudh on 22/08/14.
 */
public class ObjectArrayToList {

    public static void main(String[] args) {

        // Create sample string array
        String[] strArray = {"one", "five", "two", "three"};
        List<String> strList = Arrays.asList(strArray);

        //Iterate over the String array
        for (String str : strList) {
            System.out.print(str);
        }

        //Create sample Integer Array
        Integer[] integerArray = {23, 56, 2, 54, 2, 0};
        List<Integer> integerList = Arrays.asList(integerArray);

        //Iterate over the integer array
        for (Integer element : integerList) {
            System.out.print(element);
        }
    }
}

Firstly, I created an array of String Objects, which I have used to convert into a List of Strings.
 
In order to convert this String array into a List of String we will pass the array as an argument into the Arrays.asList() method which in turn returns a list object comprising of the elements stored in the array.
 
In the example we have also used Integer wrapper class instead of primitive int.

2) Primitives Array to List.

In case we have an array of primitives to be converted into a List, we will do so by using the org.apache.commons.lang3.ArrayUtils class.

PrimitiveArrayToList

(Don’t forget to include the library org.apache.commons.lang3 , to get this to work)

package com.javacodegeeks.example;

import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Created by anirudh on 22/08/14.
 */
public class PrimitiveArrayToList {

    public static void main(String[] args) {

        //declare a primitive array
        int[] intArray = {3, 4, 5, 1, 0, 8};

        Integer[] transformedIntegerArray = ArrayUtils.toObject(intArray);
        List<Integer> transformedIntegerList = Arrays.asList(transformedIntegerArray);

        for (Integer integer : transformedIntegerList) {
            System.out.print(integer);
        }
    }
}

Here we used the method toObject in which we passed the primitive int array and it returned the array of the wrapper class Integer.
Once we had the array of Objects we can use the method Arrays.asList() to convert it into a list.

Download the Eclipse project of this tutorial:

Download
You can download the full source code of this example here : ArraytoListExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close