| by Arround The Web | No comments

How to create a list in Java

Java provides a built-in interface named list that can be implemented by different predefined classes such as LinkedList, Vector, ArrayList, etc. Java List allows us to maintain the collection’s order, store duplicate or null elements, and positional access. Moreover, we can utilize the ListIterator interface to iterate a list in forward as well as backward directions.

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:

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:

List<String> listObj = new ArrayList<>();

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:

List<Integer> listObj = new LinkedList<>();

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:

List<String> stackObj = new Stack<>();

How to create a String-type vector?
The following snippet will show you how to create/declare an Integer-type vector in Java:

List<Integer> vectorObj = new Vector<>();

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:

List<dataType> objectName = new className<>();

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:

List<dataType> listObj = new ClassName<>(Arrays.asList(value1, value2, ...));

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?

List<String> listObj = new ArrayList<>();
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?

List<Integer> listObj = new LinkedList<>(
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.

Share Button

Source: linuxhint.com

Leave a Reply