List in Java

The List interface in Java is a part of the Java Collection Framework and is a powerful tool for working with collections of objects. It is an ordered collection of elements and allows for duplicate values. In this blog post, we will discuss what the List interface is, how it works, and how to use it in Java, including examples of its most commonly used methods.

What is List interface in Java?

A List is an ordered collection of elements that allows for duplicate values. It is implemented by various classes such as ArrayList, LinkedList, and Vector. These classes all provide slightly different implementations, but they all adhere to the basic principles of a List.

A List allows us to add, remove, and retrieve elements in a specific order. It provides methods such as add(), remove(), get(), and set() that allow us to work with the elements in the List. Additionally, a List also provides methods such as size() and isEmpty() that allow us to check the number of elements in the List and whether it is empty.

Why use List interface in Java?

The List interface is useful in situations where we need to work with a collection of objects and need to maintain the order of the elements. It is particularly useful when we need to perform operations such as adding and removing elements, or retrieving elements based on their position in the List.

List interface is also useful when we need to maintain a collection of elements in a specific order, such as a list of items in a shopping cart or a list of tasks in a to-do list.

How to use List interface in Java?

Using the List interface in Java is relatively simple. To create a List, we first need to import the java.util package, which contains the List interface.

Create and Add data into List

In the following example, we can see how to create a list object by using ArrayList which is one of the implementation of the List interface.

import java.util.ArrayList;

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

The output of the above example will be:

[1, 2, 3]

Once we have created the List, we can add data to it using the add() method. In the example above, we are adding three elements to the List.

Get data from the list

We can also retrieve data from a List using the get() method. For example, to retrieve the second element in the List, we can use the following code:

Integer value = list.get(1);

Output of this example:

2

In addition to the add() and get() methods, List also provides other useful methods such as remove(), clear(), and size().

Remove data from list

To remove data from the list, we can use the remove(int i) method. Let’s see how to remove data from the list in the following example:

list.remove(1);

The output of this example will be:

[1, 3]

Here, the item from the list is removed based on the given index. In the above example we removed the data from the index 1 which is second element. Hence the output after removeing the index 1 value is [1 and 3].

Make list as empty

We also have a method called clear() to make our list empty. Let’s see an example code below:

list.clear();

The output of this example:

[]

We removed all the items from the list by using clear() method. Hence, the output is empty.

Check the list size

In the list, we can count the number of element in the list by using the size() method. Let’s see an example below:

System.out.println(list.size());

The output of this example:

0

In the above example, we used the size() method. The size() method returns the int. We’ve used the list.clear() in our previous example so that the output is displaying 0. If we’ve used size() method before using the clear() method it would print the output value as 2.

Conclusion

The List interface is a powerful tool for working with collections of objects in the Java programming language. It provides an ordered collection of elements that allows for duplicate values. It is implemented by various classes such as ArrayList, LinkedList, and Vector, and each of these classes provides slightly different implementations, but they all adhere to the basic principles of a List.