What is List in Java?

In Java, a List is a collection that represents an ordered sequence of elements. Lists can contain duplicate elements and allow for various operations such as inserting, deleting, and accessing elements based on their position in the list. The List interface provides a way to work with ordered collections and can be implemented by various classes such as ArrayList, LinkedList, and Vector.

Lists are widely used in Java programming due to their flexibility and ease of use. They provide a dynamic way to manage data collections unlike arrays, which have a fixed size.

Here’s an example of how to use a List in Java:

import java.util.*; public class ListExample { public static void main(String[] args) { // Creating a List List fruits = new ArrayList<>(); // Adding elements to the List fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); // Accessing elements in the List System.out.println("First fruit: " + fruits.get(0)); // Removing an element from the List fruits.remove("Banana"); // Printing the updated List System.out.println("Fruits List: " + fruits); } }

List Java ArrayList LinkedList data structure collections