Pages

Wednesday, January 8, 2020

Java Program To Convert A Primitive Array To A List

1. Overview

In this programming tutorial, You will learn how to convert any primitive type array to a List. Primitive types are int, float, double, byte, long.
By end of this article, you will be able to convert int[] array to List<Integer> and float[] array to List<Float> or any primitive array to any List implementation.

Example programs are shown in classic java and using java 8 streams.

Java Program To Convert A Primitive Array To A List




2. Class Java Example


Example program to convert int[] array to ArrayList<Integer> and float[] array to LinkedList<Float>.

package com.javaprogramto.engineering.programs.conversions;

/**
 * classic java example to convert int[] array to List<Integer> and float[] array to LinkedList<Float>
 * 
 */
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PrimitiveToListClassic {

    public static void main(String[] args) {

        // int array to ArrayList
        int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 90 };

        List<Integer> intList = new ArrayList<>();
        for (int i = 0; i < intArray.length; i++) {
            intList.add(intArray[i]);

        }
        System.out.println("intList : " + intList);

        // float array to LinkedList
        float[] floatArray = { 1.1f, 2.5f, 5.3f, 4.5f, 5, 6, 7, 8, 9, 90 };

        List<Float> floatList = new LinkedList<>();
        for (int i = 0; i < floatArray.length; i++) {
            floatList.add(floatArray[i]);

        }
        System.out.println("floatList : " + floatList);

    }

}

Output:

intList : [1, 2, 3, 4, 5, 6, 7, 8, 9, 90]
floatList : [1.1, 2.5, 5.3, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 90.0]

3. Example Using Java 8 Streams


Java 8 Stream exmaple program to covnert int[] or double[] array to List<Integer> or List<Double>.

package com.javaprogramto.engineering.programs.conversions;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PrimitiveToListJava8 {

    public static void main(String[] args) {

        // int array to ArrayList
        int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 90 };

        List<Integer> intList = Arrays.stream(intArray).boxed().collect(Collectors.toList());

        System.out.println("intList : " + intList);

        // double array to LinkedList
        double[] doubleArray = { 1.1f, 2.5f, 5.3f, 4.5f, 5, 6, 7, 8, 9, 90 };

        List<Double> doubleList = Arrays.stream(doubleArray).boxed().collect(Collectors.toList());

        System.out.println("doubleList : " + doubleList);

    }

}


Output:

intList : [1, 2, 3, 4, 5, 6, 7, 8, 9, 90]
doubleList : [1.100000023841858, 2.5, 5.300000190734863, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 90.0]

boxed() method does convertion to Stream<Double> and Stream<Integer>.


4. Conclusion


In this short article, We've seen how to convert int or float or double array to List<Integer> or List<Double> or List<Float>.

Note: Arrays.asList() is not directly used to convert to List becuase it expects to return List<int[]>.

Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.