1. Lists-Srini

What is a List?

  • Ordered Collection: Elements are stored in a specific sequence.
  • Indexed Access: Elements can be accessed via their index.
  • Duplicates Allowed: Lists can contain multiple instances of the same element.
  • Insertion Order Preserved: The order of insertion is maintained.

Common Operations

  • add(E e): Adds the element to the end of the list.
  • add(int index, E element): Inserts the element at the specified index.
  • get(int index): Retrieves the element at the specified index.
  • set(int index, E element): Replaces the element at the specified index.
  • remove(int index): Removes the element at the specified index.
  • indexOf(Object o): Finds the first occurrence of the specified element.
  • lastIndexOf(Object o): Finds the last occurrence of the specified element.
  • subList(int fromIndex, int toIndex): Returns a sublist from the specified range of indices.

Example: Basic List Operations

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


List<String> fruits = new ArrayList<>();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");

System.out.println("Fruits List: " + fruits);

String firstFruit = fruits.get(0);
System.out.println("First Fruit: " + firstFruit);

fruits.set(1, "Blueberry");
System.out.println("Updated List: " + fruits);

fruits.remove(2);
System.out.println("List After Removal: " + fruits);

int index = fruits.indexOf("Mango");
System.out.println("Index of Mango: " + index);

int size = fruits.size();
System.out.println("Size of List: " + size);

boolean hasApple = fruits.contains("Apple");
System.out.println("List contains Apple: " + hasApple);

List<String> sublist = fruits.subList(0, 2);
System.out.println("Sublist: " + sublist);


Fruits List: [Apple, Banana, Orange, Mango]
First Fruit: Apple
Updated List: [Apple, Blueberry, Orange, Mango]
List After Removal: [Apple, Blueberry, Mango]
Index of Mango: 2
Size of List: 3
List contains Apple: true
Sublist: [Apple, Blueberry]

Example: List Iteration

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

List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");

System.out.println("Using for-each loop:");
for (String fruit : fruits) { 
    System.out.println(fruit);
}

System.out.println("\nUsing regular for loop:");
for (int i = 0; i < fruits.size(); i++) {
    System.out.println(fruits.get(i));
}

System.out.println("\nUsing Java 8 forEach with Lambda:");
fruits.forEach(fruit -> System.out.println(fruit));

Using for-each loop:
Apple
Banana
Orange
Mango

Using regular for loop:
Apple
Banana
Orange
Mango

Using Java 8 forEach with Lambda:
Apple
Banana
Orange
Mango

How Extending Iterable Ties into Collection

  • The Collection interface extends Iterable, meaning all collections (like List, Set, etc.) can be iterated over.
  • By implementing the Iterable interface, collections provide the iterator() method, which is used in the enhanced for-loop (for-each loop).
  • This allows collections to be traversed without needing to manually manage the index, simplifying iteration.
  • The forEach method, introduced in Java 8, also leverages Iterable to iterate over elements using lambda expressions.