$show=/label

Array to List: Program to convert Array to List in Java 8

SHARE:

A quick programming guide to convert Array to List in Java 8. Exmaple programs on Native Generics approach, Arrays.asList(), Collections.addAll() and Java 8 Stream API Arrays.stream(array).collect(Collectors.toList()).

1. Introduction


In this tutorial, you will learn how to convert an Array to List in java.

An array is a group of same type variables that hold a common name. Array values are accessed by an index that starts from 0. Arrays can hold primitive types and objects based on how the array is defined. If the array is created to store the primitive values then these are stored in the contiguous memory locations whereas objects are stored in the heap memory.

Java.util.List is sub-interface to the Collection interface. List is intended to store the values based on the index and accessed through index only. But, duplicate values can be stored in the list. List interfaces implementations are AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector.

List objects are accessed through the iterator() and listiterator() methods.

Array to List: Program to convert Array to List in Java 8


Let us dive into our core article to convert Array to List and this can be done in the following 4 ways.

  • Native Generics Apparoach
  • Arrays.asList()
  • Collections.addAll
  • Java 8 Stream API - Arrays.stream().collect()
  • Java 8 Stream boxed() for primitive arrays


Read more on How to convert Sting to Arraylist using Arrays.asList().


Input 1 : Array -> {"java", "program", "to.com", "is a ", "java portal"}
Output 1 : List -> {"java", "program", "to.com", "is a ", "java portal"}

Input 2 : Array -> {10, 20, 30, 40, 50}
Output 2 : List -> {10, 20, 30, 40, 50}


All Examples :


 // 1. Example on Arrays.asList()
 List<T> outputList = Arrays.asList(array);

  // 2. Example on Collections.addAll()
  Collections.addAll(outputList, array);
 
  // 3. Example on Arrays.stream().collect()
  List<T> outputList = Arrays.stream(array).collect(Collectors.toList());
 
  // 4. Example on boxed() method for primitive int array
  List<Integer> outputList = Arrays.stream(array).boxed().collect(Collectors.toList());

2. Convert List to Array using Native Generic Approach


package com.javaprogramto.java8.arraytolist;

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

public class ArrayToListExample {

    public static void main(String[] args) {

        String[] intArray = {"10", "20", "30", "40", "50"};
        System.out.println("Array : " + Arrays.toString(intArray));

        List<String> list = convertArrayToList(intArray);

        System.out.println("COnverted ArrayList : " + list);

    }

    private static <T> List<T> convertArrayToList(T[] array) {

        List<T> outputList = new ArrayList<T>();
        for (T t : array) {

            outputList.add(t);

        }

        return outputList;
    }

}

Output:
Array : [10, 20, 30, 40, 50]
COnverted ArrayList : [10, 20, 30, 40, 50]

3. Convert List to Array Using Arrays.asList()


Arrays class has a utility static method asList() which takes an array as input and returns a list object with array values.

package com.javaprogramto.java8.arraytolist;

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

public class ArrayToListAsListExample {
    public static void main(String[] args) {

        String[] intArray = {"java", "program", "to.com", "is a ", "java portal"};
        System.out.println("Array : " + Arrays.toString(intArray));

        List<String> list = convertArrayToList(intArray);

        System.out.println("Converted ArrayList : " + list);

    }

    private static <T> List<T> convertArrayToList(T[] array) {

        List<T> outputList = new ArrayList<T>();

        outputList = Arrays.asList(array);
        return outputList;
    }

}


Output:
Array : [java, program, to.com, is a , java portal]
Converted ArrayList : [java, program, to.com, is a , java portal]

4. Convert List to Array Using Collections.addAll()


Collections class has a utility static method addAll() which takes two parameters. The first argument is the Output List and the second argument is the array of values. This method converts the second argument array to List. It populates the values from array to the given output list.

package com.javaprogramto.java8.arraytolist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayToListCollectionsAddAllExample {
    public static void main(String[] args) {

// Example using Collections.addAll(outputList, array);

        String[] intArray = {"java", "program", "to.com", "is a ", "java portal"};
        System.out.println("Array : " + Arrays.toString(intArray));

        List<String> list = convertArrayToList(intArray);

        System.out.println("Converted ArrayList : " + list);

    }

    private static <T> List<T> convertArrayToList(T[] array) {

        List<T> outputList = new ArrayList<T>();

        Collections.addAll(outputList, array);

        return outputList;
    }

}

Output:
Array : [java, program, to.com, is a , java portal]
Converted ArrayList : [java, program, to.com, is a , java portal]

5. Java 8 Convert Array to List using Arrays.stream().collect()


Java 8 Stream API has added with a stream() method in the Arrays class to provide arrays support to Streams.

First, Convert the array into Stream by using Arrays.stream()
Second, Convert Stream into list using Collectors.toList() method.
Finally, Collect the converted stream into list using stream.collect() method.


package com.javaprogramto.java8.arraytolist;

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

public class ArrayToListJava8Stream {
    public static void main(String[] args) {

// Example using Collections.addAll(outputList, array);

        String[] intArray = {"java", "program", "to", "convert", "array to list"};
        System.out.println("Array : " + Arrays.toString(intArray));

        List<String> list = convertArrayToList(intArray);

        System.out.println("Converted ArrayList : " + list);

    }

    private static <T> List<T> convertArrayToList(T[] array) {

// convert array to stream
        Stream<T> stream = Arrays.stream(array);

// collecting converted stream into list
        List<T> outputList = stream.collect(Collectors.toList());

        return outputList;
    }

}


Output:
Array : [java, program, to, convert, array to list]
Converted ArrayList : [java, program, to, convert, array to list]

6. Java 8 Convert Primitive Array to List using boxed() method


Stream api is added with boxed() method to work with primitive collections such as int[], float[] and double[] arrays.

boxed() method converts primitive values to its wrapper objects.

package com.javaprogramto.java8.arraytolist;

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

public class ArrayToListJava8Boxed {
    public static void main(String[] args) {

// Example using stream.boxed()

        int[] intArray = {1, 2, 3, 4, 5, 6, 6};
        System.out.println("Array : " + Arrays.toString(intArray));

        List<Integer> list = convertPrimitiveArrayToList(intArray);

        System.out.println("Converted ArrayList : " + list);

    }

    private static List<Integer> convertPrimitiveArrayToList(int[] array) {

// convert int array to Integer stream with boxed() method
        Stream<Integer> stream = Arrays.stream(array).boxed();

// collecting converted stream into list
        List<Integer> outputList = stream.collect(Collectors.toList());

        return outputList;
    }

}
Output:
Array : [1, 2, 3, 4, 5, 6, 6]
Converted ArrayList : [1, 2, 3, 4, 5, 6, 6]

7. Conclusion


In this article, you've seen all possible ways in the Java traditional and java 8 stream api.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,14,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,81,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,2,Java Programs,72,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,10,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Array to List: Program to convert Array to List in Java 8
Array to List: Program to convert Array to List in Java 8
A quick programming guide to convert Array to List in Java 8. Exmaple programs on Native Generics approach, Arrays.asList(), Collections.addAll() and Java 8 Stream API Arrays.stream(array).collect(Collectors.toList()).
https://1.bp.blogspot.com/-FbJTh2BUeiU/XwNSS3DkWJI/AAAAAAAACz8/TqtZWmkSLgsr3VcSGL67cE1JHH6AazOEACLcBGAsYHQ/s640/Array%2Bto%2BList-%2BProgram%2Bto%2Bconvert%2BArray%2Bto%2BList%2Bin%2BJava%2B8.png
https://1.bp.blogspot.com/-FbJTh2BUeiU/XwNSS3DkWJI/AAAAAAAACz8/TqtZWmkSLgsr3VcSGL67cE1JHH6AazOEACLcBGAsYHQ/s72-c/Array%2Bto%2BList-%2BProgram%2Bto%2Bconvert%2BArray%2Bto%2BList%2Bin%2BJava%2B8.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/07/arrays-to-list-program-to-convert-array-to-list-in-java-8.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/07/arrays-to-list-program-to-convert-array-to-list-in-java-8.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content