Collections Framework · Lesson 73 of 150
List
List is an interface in the Collection Framework that represents an ordered group of elements, where duplicate values are allowed, and each element has a specific position (index), just like an array.
01What is a List?
List is an interface in the Collection Framework that represents an ordered group of elements, where duplicate values are allowed, and each element has a specific position (index), just like an array.
02Why is it used?
List is used whenever the order of elements matters, and duplicates are acceptable — like maintaining a list of items in a shopping cart, where the same item could appear more than once.
03Real-Life Example
Think of a to-do list written on paper. Items are in a specific order, and you could accidentally write the same task twice — both are perfectly fine for a List.
04Syntax
javaList<DataType> listName = new ArrayList<>(); // or LinkedList, Vector, etc.
05Example Program
javaimport java.util.List; import java.util.ArrayList; public class ListDemo { public static void main(String[] args) { List<String> tasks = new ArrayList<>(); tasks.add("Read"); tasks.add("Write"); tasks.add("Read"); // duplicate allowed System.out.println(tasks); } }
Output:
[Read, Write, Read]