Codementor Events

List in Java | java List Interface with Examples

Published Jul 26, 2019
List in Java | java List Interface with Examples

Java programming language has optimized data structure support. With a greater ability, it becomes fairly important to be able to control the data structure to fulfill various dependencies. List in Java is a sub-interface of the collection interface that gives optimal solutions with concepts like positional access, iteration, etc. In this article, we will discuss various operations on a list interface in Java. Following are the topics discussed in this blog:

List Interface In Java

List interface in Java is a sub-interface of the Java collections interface. It is ordered and allows duplicate entries with the flexibility of positioning while insertion and deletion. We can access the elements with the help of indexes, which also helps in searching operations as well.

The list is implemented by ArrayList, LinkedList,Vectors and Stack classes. Following is the syntax to implement the list interface in Java.

public interface List<E> extends Collection<E>

Java List Class Diagram

2019-07-24-12_48_35-Window.png

List interface extends the collection interface which extends the iterator interface. Abstract-list provides an optimized implementation of the List interface to reduce the effort. Following are the methods that are at our disposal when we use the list interface in Java.

List Interface Methods With Description

Capture.PNG
Capture1.PNG

Operations On List In Java

We can perform various operations on a list using different methods. These operations include positional access, searching operation, iteration, etc. Following are a few examples to show the operations on a list in Java.

Creating List Objects

Creating a list object is similar to creating conventional objects. Following is an example to make list objects in Java.

List a = new Stack();
List b = new Vector();
List c = new ArrayList();
List d = new LinkedList();
//After the release of generics, we can restrict the type of the object as well.
List <object> list = new ArrayList<object>();

Positional Access

Following is an example to show positional access on a list in Java.

import java.util.*;

public class Demo {
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer> ();
list.add(0,1);
list.add(1,3);
list.add(2,5);
list.add(3,7);
System.out.println(list);
list.remove(3);
System.out.println(list.get(2));
list.set(3,5);
System.out.println(list);
}
}

Searching is easier with indexes. Following is an example to show searching operation on a list in Java.

import java.util.*;

public class Demo{
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("Edureka");
list.add("Java Programming");
list.add("J2EE");
System.out.println(indexOf("Java Programming"));
System.out.println(lastIndexOf("Edureka"));
System.out.println(indexOf("Advance Java"));
}
}

Iteration

ListIterator is used to iterate over a list sequence in Java. It is bidirectional in nature. Following are a few methods for ListIterator in Java.
Capture.PNG
Capture1.PNG

ListIterator Interface

Capture.PNG

Declaration

public interface ListIterator<E> extends Iterator<E>

ListIterator Example

import java.util.*;
public class Demo{
public static void main(String[] args){
List<String> list = new Arraylist<String>();
list.add("Edureka");
list.add("Java");
list.add("J2EE");
list.add("Advance java");

ListIterator<String> li = list.listIterator();
System.out.println("Forward iteration");
while(li.hasNext()){
System.out.println("index=" + li.nextIndex() + "value=" + li.next());
}
System.out.println("backward iteration");
while(li.hasPrevious()){
System.out.println("index= " + li.previousIndex() + "value=" +li.previous());
}
}
}

Range-view

List interface provides methods to get a list view of portions of the list. Following is an example to show a range view operation.

import java.util.*;
public class Demo{
public static void main(){
List<String> list = new ArrayList<String>();
list.add("Edureka");
list.add("Edureka Java");
list.add("Java Programming");
list.add("J2EE");
list.add("Advance Java");

List <String> list2 = new ArrayList<String> ();
list2 = list.subList(2,4);
System.out.println(list2);
}
}

In this article, we have discussed various examples which include operations on a list interface in Java. Optimization leads to efficiency and with all the methods in support of the list interface, it becomes easier for any developer to work with lists for better results.

Got a question for us? please mention this in the comments of this article on “List in Java” and we will get back to you as soon as possible.

Discover and read more posts from Waseem
get started
post commentsBe the first to share your opinion
Show more replies