Java Convert Array to List

In this post, we will see how to convert array to list in java.

There are may ways to convert array to list:

By using Arrays.asList():

This is an easy method to convert Array into list.We just need to pass array as parameter to asList() method.It is a Static method available in Arrays class so, called it by class name Arrays.asList().

Syntax:

It returns a fixed size List as of size of given array.Since returned List is fixed-size we cannot add more elements.If we do so it throws UnsupportedOperationException And T represent generics means any type of Array.

Example:

OUTPUT:

Elements of Array are:
[81] [82] [83] [84] [85] Elements of list are: [81,82,83,84,85]

Using Collections.addAll() method :

This method is available in Collections class.Remember that the array and List should be of same type.

Syntax:

public static boolean addAll(Collection c,T x)

Where c is a collection into which value is to be insert and x is an array contains values to be insert in c.

OUTPUT:

Elements of Array are:
[81] [82] [83] [84] [85] Elements of list are: [81,82,83,84,85]

Using Java 8’s Stream

We can use Java 8’s Stream to convert an array to List.

OUTPUT:

Elements of Array are:
1
3
5
6
10
Elements of list are: [1,3,5,6,10]

Manual way using add() method:

We use add() method inside loop to convert Array into List.
Example :

OUTPUT:

Elements of Array are:
[81] [82] [83] [84] [85] Elements of list are: [81,82,83,84,85]

That’s all about Converting the array to List in java.

Was this post helpful?

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusionWas this post helpful? Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusionWas this post helpful? When developing applications in Java there are many cases where we will find ourselves converting data from one type […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floorWas this post helpful? Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using IterablesWas this post helpful? In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.DateWas this post helpful? In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.