List in Java | Interface, Methods, Example

Last updated on August 3rd, 2024

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.

8 thoughts on “List in Java | Interface, Methods, Example”

  1. Have you ever thought about publishing an ebook or guest authoring on other sites? I have a blog centered on the same ideas you discuss and would really like to have you share some stories/information. I know my subscribers would appreciate your work. If you are even remotely interested, feel free to shoot me an e mail.

    Reply
  2. It’s amazing to pay a quick visit this web page and reading the views of all colleagues concerning this article, while I am also zealous of getting experience.

    Reply
  3. It’s actually a cool and useful piece of info. I am satisfied that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.

    Reply
  4. What i don’t realize is in truth how you’re not actually a lot more smartly-preferred than you might be now. You’re so intelligent. You already know therefore considerably on the subject of this subject, produced me in my opinion consider it from numerous various angles. Its like women and men don’t seem to be involved unless it is one thing to accomplish with Girl gaga! Your individual stuffs outstanding. All the time deal with it up!

    Reply
  5. Hi i am kavin, its my first occasion to commenting anyplace, when i read this article i thought i could also make comment due to this sensible article.

    Reply
  6. I am extremely inspired together with your writing talents and also with the layout for your weblog. Is this a paid subject or did you modify it yourself? Anyway stay up the nice high quality writing, it is rare to see a nice weblog like this one these days..

    Reply

Leave a Comment