Java List [With Examples]

Introduction

Java List interface is used to hold the ordered collection. It is a Collection’s child interface. It is an ordered collection of objects that can store duplicate values. Because List keeps the insertion order, it enables positional access and element insertion.

The Collection interface is inherited by the List interface, located in java.util package. It is a ListIterator interface constructor. We may iterate the list in both forward and backward directions using the ListIterator. The List interface’s implementation classes include ArrayList, LinkedList, Stack, and Vector. In Java programming, ArrayList and LinkedList are often used. Since Java 5, the Vector class has been abandoned.

list flow

Declaration:

public interface List<E> extends Collection<E> ; 

Basic Operations of Java List Interface

The List contains a number of methods for performing various operations on ArrayList. In this section, we will look at several often used List operations

  1. Add elements
  2. Access elements
  3. Change or Replace elements
  4. Remove elements

Operation 01: Java List Add Elements

Code 01: Adding elements to List class using add() method

// Importing all utility classes
import java.util.*;

// Main class
class Main {

	// Main driver method
	public static void main(String args[])
	{
		// Creating an object of List interface,
		// implemented by ArrayList class
		List<String> list = new ArrayList<>();

		// Adding elements to object of List interface
		// Custom elements
		list.add(0, "Apple");
		list.add(1, "Banana");
	    list.add(2, "Mango");

		// Print all the elements inside the
		// List interface object
		System.out.println(list);
	}
}

Output:

[Apple, Banana, Mango]

Operation 02: Access Elements

Code 02: The get() function of the List class is used to access an element.

// Java Program to Add Elements to a List

// Importing all utility classes
import java.util.*;

// Main class
class Main {

	// Main driver method
	public static void main(String args[])
	{
		// Creating an object of List interface,
		// implemented by ArrayList class
		List<String> list = new ArrayList<>();

		// Adding elements to object of List interface
		// Custom elements
		list.add(0, "Apple");
		list.add(1, "Banana");
	    list.add(2, "Mango");

		// Print all the elements inside the
		// List interface object
		System.out.println("ArrayList: " + list+"\n");
		
		// get the element from the arraylist
        String str = list.get(1);
        System.out.print("Element at index 1: " + str);
	}
}

Output:

ArrayList: [Apple, Banana, Mango]

Element at index 1: Banana

Operation 03: Java List Replace at Index

Code 03: The set() function of the List class is used to change or replace the items

// Java Program to Add Elements to a List

// Importing all utility classes
import java.util.*;

// Main class
class Main {

	// Main driver method
	public static void main(String args[])
	{
		// Creating an object of List interface,
		// implemented by ArrayList class
		List<String> list = new ArrayList<>();

		// Adding elements to object of List interface
		// Custom elements
		list.add(0, "Apple");
		list.add(1, "Banana");
	    list.add(2, "Mango");

		// Print all the elements inside the
		// List interface object
		System.out.println("ArrayList: " + list+"\n");
		
        // change the element of the array list
        list.set(1, "Orange");
        System.out.println("Modified ArrayList: " + list);
	}
}

Output:

ArrayList: [Apple, Banana, Mango]
Modified ArrayList: [Apple, Orange, Mango]

Operation 04: Java List Remove Elements

Code 04: In order to remove an element from a list, we can use the remove() method.

// Java Program to Add Elements to a List

// Importing all utility classes
import java.util.*;

// Main class
class Main {

	// Main driver method
	public static void main(String args[])
	{
		// Creating an object of List interface,
		// implemented by ArrayList class
		List<String> list = new ArrayList<>();

		// Adding elements to object of List interface
		// Custom elements
		list.add(0, "Apple");
		list.add(1, "Banana");
	    list.add(2, "Mango");

		// Print all the elements inside the
		// List interface object
		System.out.println("ArrayList: " + list+"\n");
		
        // remove element from index 2
        String str = list.remove(1);
        System.out.println("Updated ArrayList: " + list);
        System.out.println("Removed Element: " + str);
	}
}

Output:

ArrayList: [Apple, Banana, Mango]

Updated ArrayList: [Apple, Mango]
Removed Element: Banana

Classes that Implement List

We cannot generate objects from the List since it is an interface. We may utilize the following classes to access List interface functionality:

Class 01: Implementing the ArrayList Class

Code 05:

import java.util.List;
import java.util.ArrayList;

class Main {

    public static void main(String[] args) {
        // Creating list using the ArrayList class
        List<String> list = new ArrayList<>();

        // Add elements to the list
        list.add("Apple");  
        list.add("Mango");  
        list.add("Banana");  

        System.out.println("List: " + list);

        // Access element from the list
        String str = list.get(1);
        System.out.println("Accessed Element: " + str+"\n");

        // Remove element from the list
        String removedstr = list.remove(1);
        System.out.println("Removed Element: " + removedstr);
    }
}

Output:

List: [Apple, Mango, Banana]
Accessed Element: Mango

Removed Element: Mango

Class 02: Implementing the LinkedList Class

Code 06:

import java.util.List;
import java.util.LinkedList;

class Main {

    public static void main(String[] args) {
        // Creating list using the LinkedList class
        List<String> list = new LinkedList<>();

        // Add elements to the list
        list.add("Apple");  
        list.add("Mango");  
        list.add("Banana");  

        System.out.println("List: " + list);

        // Access element from the list
        String str = list.get(1);
        System.out.println("Accessed Element: " + str+"\n");

        // Remove element from the list
        String removedstr = list.remove(1);
        System.out.println("Removed Element: " + removedstr);
    }
}

Output:

List: [Apple, Mango, Banana]
Accessed Element: Mango

Removed Element: Mango

Class 03: Implementing the Vector Class

Code 07:

import java.util.*;

class Main {

    public static void main(String[] args) {
        // Creating list using the Vector class
        List<String> list = new Vector<>();

        // Add elements to the list
        list.add("Apple");  
        list.add("Mango");  
        list.add("Banana");  

        System.out.println("List: " + list);

        // Access element from the list
        String str = list.get(1);
        System.out.println("Accessed Element: " + str+"\n");

        // Remove element from the list
        String removedstr = list.remove(1);
        System.out.println("Removed Element: " + removedstr);
    }
}

Output:

List: [Apple, Mango, Banana]
Accessed Element: Mango

Removed Element: Mango

Class 04: Implementing the Stack Class

Code 08:

import java.util.*;

class Main {

    public static void main(String[] args) {
        // Creating list using the Stack class
        List<String> list = new Stack<>();

        // Add elements to the list
        list.add("Apple");  
        list.add("Mango");  
        list.add("Banana");  

        System.out.println("List: " + list);

        // Access element from the list
        String str = list.get(1);
        System.out.println("Accessed Element: " + str+"\n");

        // Remove element from the list
        String removedstr = list.remove(1);
        System.out.println("Removed Element: " + removedstr);
    }
}

Output:

List: [Apple, Mango, Banana]
Accessed Element: Mango

Removed Element: Mango

Java List Examples

Code 09: Below Snippet shows you Conversion from Java List to Array

import java.util.*;
public class Main {
   public static void main(String args[]){
      List<String> list = new ArrayList<String>();
      list.add("Apple");
      list.add("Orange");
      list.add("Banana");

      System.out.println("Contents of list :"+list);
      String[] myArray = new String[list.size()];
      list.toArray(myArray);

      for(int i=0; i<myArray.length; i++){
         System.out.println("Element at the index "+i+" is :"+myArray[i]);
      }
   }
}

Output:

Contents of list :[Apple, Orange, Banana]
Element at the index 0 is :Apple
Element at the index 1 is :Orange
Element at the index 2 is :Banana

Code 10: Below Snippet shows you Java List Sort Using sort() method

import java.util.*;
 
public class Main {  
public static void main(String[] args) {  
        List<String> list= new ArrayList<>(); 
        
        list.add("Apple");  
        list.add("Mango");  
        list.add("Banana");  
     
        System.out.println("List : "+list);  
        //will sort the string acc to the alphabets  
        Collections.sort(list);  
        System.out.println("Sorted List : "+list);  
    }  
}

Output:

List : [Apple, Mango, Banana]
Sorted List : [Apple, Banana, Mango]

Code 11: Below snippet shows the java list iterator example

// Java program to Iterate the Elements
// in an ArrayList

// Importing java utility classes
import java.util.*;

// Main class
public class Main {

	// main driver method
	public static void main(String args[])
	{
		// Creating an empty Arraylist of string type
		List<String> list = new ArrayList<>();

		// Adding elements to above object of ArrayList
		list.add("Mango");
		list.add("Banana");

		// Adding element at specified position
		// inside list object
		list.add(1, "Apple");

		// Using for loop for iteration
		for (int i = 0; i < list.size(); i++) {

			// Using get() method to
			// access particular element
			System.out.print(list.get(i) + " ");
		}

		// New line for better readability
		System.out.println();

		// Using for-each loop for iteration
		for (String str : list)

			// Printing all the elements
			// which was inside object
			System.out.print(str + " ");
	}
}

Output:

Mango Apple Banana 
Mango Apple Banana 

Java List Methods

MethodDescription
add()Adds an element to a list
addAll() Adds all elements of one list to another
get() Helps to randomly access elements from lists
iterator() Returns iterator object that can be used to sequentially access elements of lists
set()Changes elements of lists
remove() Removes an element from the list
removeAll() Removes all the elements from the list
clear()It removes all the elements from the list (more efficient than removeAll())
size()Returns the length of lists
toArray() Converts a list into an array
contains()Returns true if a list contains the specified element

FAQs

Java List vs ArrayList

  • The list is an interface
  • ArrayList is a class
  • List interface extends the Collection framework
  • ArrayList extends AbstractList class and implements the List interface
  • The list cannot be instantiated.
  • ArrayList can be instantiated.

Array vs ArrayList

  • Arrays are fixed size
  • ArrayList’s size automatically adjusted
  • Arrays can hold primitives and object
  • ArrayList can hold only objects
  • Arrays can be multidimensional
  • ArrayList cannot be multi-dimensional
  • An array is a build-in data structure
  • ArrayList is implementing a class of List interface in the Collection framework

Java List vs Set

The Collection interface is inherited by both the List and the Set interfaces. However, there is a considerable distinction between them.

  • Lists may include duplicate elements. Sets, on the other hand, cannot have duplicate components.
  • List elements are kept in some order. Elements in sets, on the other hand, are kept in groups, much assets in mathematics.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. ArrayList Java Tutorial [With Examples]

Leave a Comment