This write-up will guide you on how to declare/create a list in Java. For a profound understanding, this post will explain the below-listed concepts of the Java lists:
- How to create/declare a List in Java?
- How to create/declare a mutable List in Java?
- Practical Implementation
So, letβs get started!
How to create/declare a List in Java?
In java, built-in classes like ArrayList, stack, LinkedList, and vector provide the implementation of the List interface. Using these classes, we can create/declare a list of any type like String, Integer, etc.
The βnewβ keyword can be used to declare a Java list. The below-given snippets will assist you in understanding how to create a list of any specific data type in Java.
How to create a list using different Built-in classes in Java?
This section will guide you on how to create/declare a list of any specific data type using different built-in classes like ArrayList, stack, etc.
How to declare a String-type ArrayList?
The below snippet shows the basic syntax to create a string-type Arraylist in Java:
How to create an Integer-type LinkedList?
The below code snippet will show you the basic syntax to create an integer-type LinkedList in Java:
How to create a String-type Stack?
Letβs have a look at the below snippet to understand how to create a string-type stack in java:
How to create a String-type vector?
The following snippet will show you how to create/declare an Integer-type vector in Java:
General Syntax
Considering the above-given syntaxes, we can make a generic syntax to create a list of any specific data type, as shown below:
Here, in place of βdataTypeβ, you can specify any data type of your choice like Integer, String, etc. while in place of βclassNameβ you can specify the name of a class that can implement the List interface like LinkedList, stack, vector, etc.
How to create/declare a List using the asList() method in Java?
The Java Arrays class provides a built-in method named asList() that can be used to create a mutable as well as an immutable list in Java.
The below-snippet shows the basic syntax to create a list using asList() method in Java:
Practical Implementation
The List interface provides a wide range of built-in methods to serve different functionalities. We can utilize any built-in method of the List interface, once the list creation is done. This section will explain how to utilize the add() method to initialize/add some elements to the list.
Example: How to create/declare a Java list?
listObj.add("John");
listObj.add("Mike");
listObj.add("Joe");
System.out.println("List of Employee Names : \n" + listObj);
In this example, firstly, we created an ArrayList using the new keyword, and afterward we utilized the add() method to insert some names into that list. Finally, we utilized the println() method to print the listβs elements:
In this way, we can add the elements to a list of any specific data type.
Example: How to create/declare a list using asList() method in Java?
Arrays.asList(10, 5, 11, 15, 13));
System.out.println("List of Employee ids: \n" + listObj);
In the above program, we created a LinkedList using the Arrays.asList() method:
This is how the asList() method works in java.
Conclusion
In Java, different built-in classes like ArrayList, stack, LinkedList, and vector provide the implementation of the List interface. Using these classes, we can create a list of any data type such as String, Integer, etc. The βnewβ keyword and the asList() method of the Arrays class can be used to create a mutable or immutable list in Java. This write-up explained a couple of ways to create a list in Java.