Mastering the List Interface in Java: A Deep Dive into Types, Features, and Practical Examples

What is the List Interface in Java?

  • The List interface in java is portion of the Java Collections Framework and speaks to an ordered collection of components.
  • It may be a subtype of the Collection interface and permits copy components whereas keeping up the arrange of addition.
  • The List interface gives a contract for any class that implements it, characterizing methods for adding, removing, and accessing components based on their index positions.
  • The List interface is fundamental in Java for working with information that requires both requesting and adaptability in component control.
  • Common executions of the List interface include ArrayList, LinkedList, and Vector. These classes give concrete usage of the different methods characterized by the List interface.

 

Key Highlights of the List Interface

1.Ordered Collection:

  • A List preserves the arrange of addition, meaning that components are stored within the arrange in which they are added.
  • You’ll be able get to components by their list position.

2.Allowing Duplicates:

  • Not at all like sets, a List allows duplicate components. The same object can show up numerous times within the list.

3.Dynamic Size:

  • A List can develop and shrink in size dynamically as components are added or removed.

4.Index-Based Access:

  • A List permits you to get to components utilizing their list, comparable to an array. This gives quick and simple retrieval of components.

 

Common Methods of the List Interface

The List interface gives a rich set of methods for controlling records. A few of the most important methods include:

1.add(E e): Adds an component to the end of the list.

2.add(int index, E component): Inserts an component at the required index.

3.get(int index): Returns the component at the required index.

4.set(int index, E component): Replaces the component at the required index with a new component.

5.remove(int index): Removes the component at the required index.

6.remove(Object o): Removes the first event of the required element.

7.size(): Returns the number of components within the list.

8.contains(Object o): Checks if the list contains a specific component.

9.indexOf(Object o): Returns the index of the first event of the required component.

10.clear(): Removes all components from the list

 

Types of List Usage in Java

There are a few commonly utilized executions of the List interface in Java, each with unique features and execution characteristics. The foremost regularly utilized are:

List Interface in Java
List Interface in Java
1. ArrayList

Description:

  • ArrayList is the most commonly utilized usage of the List interface.
  • It is supported by an array, which gives efficient random access to components.
  • ArrayList is perfect once you require quick access to components through an list and have less additions and deletions.

Performance:

  • Random access: O(1) time complexity for get() and set() methods.
  • Insertions/Deletions: O(n) time complexity for inserting/removing components (but at the end of the list, which is O(1)).
  • Example: 

import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add(“Apple”);
fruits.add(“Banana”);
fruits.add(“Orange”);

System.out.println(fruits); // Output: [Apple, Banana, Orange]

fruits.remove(“Banana”);
System.out.println(fruits); // Output: [Apple, Orange]
}
}

 

2. LinkedList

Description:

  • LinkedList is another commonly utilized usage of the List interface, but not at all like ArrayList, it is supported by a doubly linked list structure.
  • This permits for proficient additions and deletions at the starting or within the middle of the list but slower random access.

Performance:

  • Insertions/Deletions: O(1) time complexity for adding/removing components at the starting or middle (once the node is found).
  • Random access: O(n) time complexity for get() and set() methods.
  • Example:

import java.util.LinkedList;

public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> cities = new LinkedList<>();
cities.add(“New York”);
cities.add(“London”);
cities.add(“Paris”);

System.out.println(cities); // Output: [New York, London, Paris]

cities.addFirst(“Tokyo”);
cities.addLast(“Sydney”);

System.out.println(cities); // Output: [Tokyo, New York, London, Paris, Sydney]
}
}

 

3. Vector

Description:

  • Vector may be a legacy usage of the List interface, comparable to ArrayList, but it is synchronized, which makes it thread-safe.
  • In any case, it is less commonly utilized nowadays due to the accessibility of other collections, such as ArrayList and CopyOnWriteArrayList, that give better performance in most cases.

Performance:

  • Random access: O(1) time complexity for get() and set() methods.
  • Insertions/Deletions: O(n) time complexity for inserting/removing components.
  • Example:

import java.util.Vector;

public class VectorExample {
public static void main(String[] args) {
Vector<Integer> numbers = new Vector<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);

System.out.println(numbers); // Output: [10, 20, 30]

numbers.remove(1);
System.out.println(numbers); // Output: [10, 30]
}
}

 

4. Stack

Description:

  • Stack could be a subclass of Vector that implements a last-in, first-out (LIFO) stack of components.
  • It gives methods like push(), pop(), and peek() for stack-based operations.

Performance:

  • Push/Pop:O(1) time complexity.
  • Example:

import java.util.Stack;

public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push(“A”);
stack.push(“B”);
stack.push(“C”);

System.out.println(stack); // Output: [A, B, C]

stack.pop();
System.out.println(stack); // Output: [A, B]
}
}

 

 

 

Leave a Comment