Pull to refresh

List in Java | Interface, Methods, Example

Level of difficultyEasy
Reading time4 min
Views1K

In this topic, we will learn about what is a List in Java. How to create a List in Java? What are the methods of List in Java? The List is an interface in Java. It is extending the Collection interface in Java. This List interface is present in the java.util package in Java. A list represents a group of individual objects as a single entity where duplicates are allowed and insertion order is preserved.

public interface List<E> extends Collection<E>

→ In the above, we mentioned the general declaration of the List in Java.

How to create a List in Java?

The list interface has four concrete subclasses(ArrayList, LinkedList, Vector and Stack) in the Collection Framework.
We can create the list by using its concrete subclasses as follows:

List l1 = new ArrayList();
List l2 = new LinkedList();
List l3 = new Vector();
List l4= new Stack();

How to create a Generic List object in Java?

We can create the generic list object as follows:

List<data type> list = new ArrayList<data type>(); 

→ In the above, we mentioned the general syntax of creating a generic List by using the ArrayList class.

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

→ In the above create a list of objects of String type by using ArrayList class.

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

→ In the above example, we created a list of objects of Integer type by using the LinkedList class.   

List<String> list = new LinkedList<String>();

→ In the above example, we created a list of objects of String type by using the LinkedList class.

List<User> list=new ArrayList<User>(); 

→ In the above example, we created a list of objects of type obj by using the ArrayList class. The User is the type object.

 List<String> strList = new ArrayList<>();
 List<Integer> list = new LinkedList<>();

→ Java 1.7 onwards, we can use a diamond operator

What are the Methods of List?

The List interface has several methods: 
  • void add(int index, E element): This method is used to insert an object at a particular position in the list.
      E- It is a type of element in the list
For example, suppose we want to add “Peter” at the 4th position in the list we can use this method as below:

list.add(4,"Peter");

  • boolean addAll(int index, Collection<? extends E> c): This method is used to add a Collection object at the particular position in the list and shift the subsequent objects to the right by increasing their indices.
For example, suppose we want to add a list at the 2nd position in the other list we can use this method as below:

list.add("P");
list.add("Q");
list.add("R");

Now, we will create a new list using list1 reference.

list1.add("W");
list1.add("Y");

Now, we will add list1 at the 2nd position in the list.

list.add(2,list2);

  • E remove(int index): This method is used to remove an element at a specified position in the list.
For example, we consider the following:

list.remove(2);

  • E get(int index): This method is used to return an object stored at a specified position in the list.
For example, we consider the following:

list.get(1);

  • E set(int index, E element): This method is used to replace an existing element with a new element with a specified position in the list.
For example, we consider the following:

list.set(1, "T");

  • int indexOf(Object o): This method is used to return an index of the particular element of the first occurrence in the list. If the specified element is not present in the list then will return -1.
For example, we consider the following:

list.indexOf("A");

  • int lastIndexOf(Object o): This method is used to return an index of the particular element of the last occurrence in the list. If the specified element is not present in the list then will return -1.
For example, we consider the following:

list.lastIndexOf("A");

  • ListIterator<E> listIterator(): This method is used to return the listIterator of the elements in the list in a proper sequence.
  • ListIterator<E> listIterator(int index): This method is used to return a listIterator of the elements in the list in the proper sequence, starting at the specified position in the list.

Example of Create a List in Java

import java.util.ArrayList; 
import java.util.List; 
public class Test { 
public static void main(String[] args){ 
List<String> al = new ArrayList<String>();
al.add("Apple"); 
al.add("Mango"); 
al.add("Orange"); 
al.add("Grapes"); 
System.out.println("List1 contains: " +al); 
List<String> al2 = new ArrayList<String>(); 
al2.add("10"); 
al2.add("11"); 
al2.add("12"); 
System.out.println("List2 contains : "+al2); 
al.addAll(2, al2); 
System.out.println("After added List2 at 2nd index in the list: " +al); 
  } 
}

Output

List1 contains: [Apple, Mango, Orange, Grapes]
List2 contains : [10, 11, 12]
List1 after adding List2 at 2nd index: [Apple, Mango, 10, 11, 12, Orange, Grapes]

How to Iterate List in Java?

There are different ways to iterate a list in Java. You can refer to the post by clicking here

When to use List in Java?

There are the following points where we will use List:
  • When we want to need duplicate elements to store
  • When we want to allow null elements
  • When we want to preserve the insertion order

Conclusion

In this topic, we learnt about what is a List Interface in Java and its various methods.

Tags:
Hubs:
Rating0
Comments1

Articles