• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Initialize a List in Java

July 31, 2023 //  by Talha Malik

“Lists” play a significant role in Java to contain the records effectively. These are often preferred by the developers to store the data since they are dynamic in size. More specifically, “ArrayList” has various predefined methods which assist in randomly inserting and deleting the entries of multiple data types in an effective manner.

How to Initialize a List in Java?

To initialize a list in Java, apply the following approaches:

  • “Arrays.asList()” Method.
  • “List.add()” Method.
  • “Collections” Class Methods.
  • “Stream” Approach.
  • “List.of()” Method.

Approach 1: Initializing a List Using the “Arrays.asList()” Method

The “Arrays.asList()” method makes an immutable list via an array.

Syntax

List<dt> list=Arrays.asList(x);

In this syntax, “dt” refers to the list’s data type and “x” corresponds to the array elements.

Now, proceed to the following code example:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> initList = Arrays.asList(10, 20, 30, 40);
  System.out.println("Initialized List -> " + initList);
}}

In this code, import the package required to access the List functionalities. After that, apply the “Arrays.asList()” method to initialize an integer list from the array and display it.

Output

Approach 2: Initializing a List Using the “List.add()” Method

The “List.add()” method appends the target element in the argument at the last of the list. This method will be applied to initialize various sorts of lists i.e., ArrayList, LinkedList and Stack as well.

Syntax

boolean add(x)

In the given syntax, “x” is the item to be appended in this list.

Return Value

It returns true if the target item is appended.

Now, consider the below-given example:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> arraylist = new ArrayList<Integer>();
  arraylist.add(1);
  arraylist.add(2);
  System.out.println("ArrayList -> " + arraylist);
  List<Integer> linklist = new LinkedList<Integer>();
  linklist.add(10);
  linklist.add(20);
  System.out.println("LinkedList -> " + linklist);
  List<Integer> st = new Stack<Integer>();
  st.add(100);
  st.add(200);
  System.out.println("Stack -> " + st);
}}

In this code snippet:

  • Create an integer “ArrayList”, add the stated integers in it and return it.
  • After that, define an integer “LinkedList”, append the values in it as well and display it.
  • Lastly, create a “Stack” of Integer type, add the given integers in it and retrieve it.

Output

Approach 3: Initializing a List Using the “Collections” Class Methods

There are multiple methods in the “Collections” class that can be utilized to initialize a list. These methods include “addAll()”, “unmodifiableList()” and “singletonList()” which will be discussed individually via the following examples:

Example 1: Applying the “Collections.addAll()” Method to Create a List

The “addAll()” method adds all the target items to the particular collection. 

Syntax

boolean addAll(x, elem)

In the above syntax:

  • “x” points to the collection in which the elements need to be added.
  • “elem” corresponds to the elements to be added into “x”.

Return Value

It retrieves true if the collection changes.

Overview of the below-given example explaining the discussed concept:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> initlist = new ArrayList<Integer>();
  Collections.addAll(initlist, 1, 2, 3);
  System.out.println("Initialized List -> " + initlist);
}}

In this code, create an ArrayList and append all the integer values specified as “addAll()” method arguments in it (ArrayList) and display the list.

Output

Example 2: Applying the “Collections.unmodifiableList()” to Create a List

The “unmodifiableList()” method returns an unmodifiable view of the particular list. It is such that the resultant list can’t be altered.

Syntax

public static <T> ListT> unmodifiableList(List<? extends T> list)

According to this syntax, this method takes “list” as a parameter against which an unmodifiable view is to be retrieved.

The following code initializes a list:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> initlist = Collections.unmodifiableList(Arrays.asList(10, 20, 30));
  System.out.println("Initialized List -> " + initlist);
}}

This code applies the combined “unmodifiableList()” and the “Arrays.asList()” methods to transform the integer array into a list and return it.

Output

Example 3: Applying the “singletonList()” Method to Create a List

The “Collections.singletonList()” method retrieves an immutable list comprising one element only.

Syntax


Based on this syntax, this method takes the object “ob” as a parameter to be contained in the retrieved list.

Return Value

It retrieves an immutable list comprising only the specified object.

The below-stated code demonstrates the discussed concept:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> list = Collections.singletonList(1);
  System.out.println("Initialized List -> " + list);
}}

In this block of code, apply the “singletonList()” method to return a list of only the given one integer element.

Output

Approach 4: Initializing a List Using the “Stream” Approach

The “Stream” approach can also be utilized to initialize a list. The “Stream.of()” method assists in doing so by retrieving a sequential Stream comprising a single element.

Syntax

Stream of(x)

This method takes a mandatory parameter “x” which is the single element in the Stream.

Following is the code demonstration:

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Initlist {
public static void main(String args[]){
  List<Integer> initlist = Stream.of(10, 20, 30).collect(Collectors.toList());
  System.out.println("Initialized List -> "+ initlist);
}}

In this code, import the required packages to work with the stream. After that, apply the combined “Stream.of()“, “collect()” and “toList()” methods to create an integer list.

Output

Approach 5: Initializing a List Using the “List.of()” Method

The “List.of()” method takes any number of arguments and creates an unmodifiable list.

Syntax

List<dt> initlist = List.of(x);

In this syntax, “dt” refers to the list’s data type and “x” indicates the array elements.

Now, move on to the practical implementation:

import java.util.*;
public class Initlist {
public static void main(String args[]){
  List<Integer> initlist = List.of(10, 20, 30);
  System.out.println("Initialized List -> "+ initlist);
}}

In this snippet of code, apply the “List.of()” method to initialize an integer list.

Output

Conclusion

To initialize a list in Java, apply the “Arrays.asList()” method, the “List.add()” method, the “Collections” class methods, the “Stream” approach, or the “List.of()” method. The “Arrays.asList()” method is the most effective as it initializes a list directly with a minimal code.

Category: Java

Previous Post: « Does Java Support Struct
Next Post: What is the Java String format() Method »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact