Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

List in Java: One Stop Solution for Beginners

Last updated on Nov 27,2019 8.9K Views

2 / 22 Blog from Java Collections

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

list class diagram - list in java - edureka

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

MethodDescription
void add(int index, E element)It is used to insert elements at a particular position
boolean add(E e)It appends the elements at the end of the list
boolean addAll(int index, Collection<? extends E> c)It appends elements in a specified collection at the end of the list
void clear()Removes all the elements from the list
boolean equals(object o)It compares the specified object with elements in the list
int hashcode()It returns the hash code value of the list
E get(int index)It fetches elements from a particular location of the list
boolean isEmpty()It checks if the list is empty or not
int lastIndexOf(object o)Returns the index value of the specified object
Object[] toArray()It returns an array with all the elements in a list in a correct order
T[] toArray(T[] a)Returns an array with all the elements in a list
boolean contains(object o)It returns true if the specified element is present in the list
boolean containsAll(Collection<?>c)It checks for multiple elements in a list
int indexOf(Object o)Returns the index of the element in the first occurrence
E remove(int index)Removes the elements at the specified location
boolean remove(Object o)It removes the first occurrence of the specified element
boolean removeAll(Collection<?> c)Removes all elements from the list
void replaceAll(UnaryOperator operator)Replaces all elements with specified values
void retainAll(Collection<?> c)Retains all elements at a specified location
E set(int index, E element)Replaces specified element at specified location
void sort(Comparator<? Super E> c)Sorts the list based on specified comparator
Spliterator spliterator()Creates spliterator over elements
List<E> subList (int fromIndex, int toIndex)Fetches elements in a given range
int size()Returns the number of elements in a list

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);
}
}

Search

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.

ListIterator Interface

Method Description
void add(E e)Inserts element in the list
boolean hasNext()Returns true if the forward traversal has a next element
E next()Returns the next element in the list
int nextindex()Returns the next index in the list
boolean hasPrevious()Returns true if the backward traversal has a next element
E previous()Returns the previous element in the list
E previousIndex()Returns the previous index in the list
void remove()It removes the last element in the list
void set(E e)It replaces the last element with the specified value

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.

Java programming language has become one of the most promising languages nowadays, with the increasing demand it has catered to a lot of job opportunities in the IT industry. To master all the skills enroll in edureka’s Java Certification program and kick-start your career.

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.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

List in Java: One Stop Solution for Beginners

edureka.co