List

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

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button