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:

Table of Contents

πŸ’» Awesome Tech Resources:
  • Looking for βš’οΈ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events πŸ—“οΈ Calendar.️

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.

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    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 in java. Read also: Convert LocalDate to Date in java πŸ’» Awesome Tech Resources: Looking for βš’οΈ […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-607ab4f2ac97b589867732/] Create a list of movies and convert with to […]

  • 29 February

    Java long to String

    In this post, we will see how to convert long to String in java. There are lot of ways to convert long to String.Let’s see each one by one. Using Long.toString() You can use Long class toString() method to convert long to String. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our […]

  • Convert LocalDateTime to Date in Java
    15 May

    Java LocalDateTime to Date

    In this post, we will see how to convert LocalDateTime to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our job portal. Looking for tech events? Go […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.