

In this image you saw above class implements interface and interface extends interface, for example
ArrayList implements List and TreeSet implements NavigableSet extends SortedSet extends Set extends Collection.
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// Create a List using ArrayList implementation
List<Object> list = new ArrayList<>();
// Add elements to the list
list.add("apple");
list.add(10);
list.add(null);
list.add("banana");
list.add(10); // Adding a duplicate element
list.add(3.14); // Adding a heterogeneous element
list.add(null); // Adding another null element
// Print the list
System.out.println("List elements: " + list);
// Access elements by index
System.out.println("Element at index 0: " + list.get(0));
System.out.println("Element at index 2: " + list.get(2));
// Modify an element at a specific index
list.set(1, "orange");
System.out.println("List after modification: " + list);
// Remove an element by index
list.remove(3);
System.out.println("List after removing element at index 3: " + list);
// Iterate over the list
System.out.println("Iterating over the list:");
for (int i = 0; i < list.size(); i++) {
System.out.println("Element at index " + i + ": " + list.get(i));
}
// Check if the list contains a specific element
boolean containsApple = list.contains("apple");
System.out.println("List contains 'apple': " + containsApple);
// Check the index of a specific element
int indexOfNull = list.indexOf(null);
System.out.println("Index of first null element: " + indexOfNull);
}
}
List elements: [apple, 10, null, banana, 10, 3.14, null]
Element at index 0: apple
Element at index 2: null
List after modification: [apple, orange, null, banana, 10, 3.14, null]
List after removing element at index 3: [apple, orange, null, 10, 3.14, null]
Iterating over the list:
Element at index 0: apple
Element at index 1: orange
Element at index 2: null
Element at index 3: 10
Element at index 4: 3.14
Element at index 5: null
List contains 'apple': true
Index of first null element: 2
The List interface in Java extends the Collection interface and includes additional methods specifically for manipulating elements based on their positions (indices) in the list. Here are the methods that are specific to the List interface, excluding those inherited from the Collection interface:
Method Signature | Description |
---|---|
void add(int index, E element) | Inserts the specified element at the specified position in the list. |
boolean addAll(int index, Collection<? extends E> c) | Inserts all elements from the specified collection at the specified position. |
E get(int index) | Returns the element at the specified position in the list. |
int indexOf(Object o) | Returns the index of the first occurrence of the specified element. |
int lastIndexOf(Object o) | Returns the index of the last occurrence of the specified element. |
ListIterator<E> listIterator() | Returns a list iterator over the elements in the list. |
ListIterator<E> listIterator(int index) | Returns a list iterator starting at the specified position. |
E remove(int index) | Removes the element at the specified position in the list. |
E set(int index, E element) | Replaces the element at the specified position with the specified element. |
List<E> subList(int fromIndex, int toIndex) | Returns a view of the specified range within the list. |
Returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain the element.
xxxxxxxxxx
List<String> list = Arrays.asList("A", "B", "C", "B");
System.out.println(list.lastIndexOf("B")); // Output: 3
Returns a list iterator over the elements in the list (in proper sequence).
xxxxxxxxxx
List<String> list = Arrays.asList("A", "B", "C");
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// Output: A B C
Returns a list iterator over the elements in the list (in proper sequence), starting at the specified position in the list.
xxxxxxxxxx
List<String> list = Arrays.asList("A", "B", "C");
ListIterator<String> iterator = list.listIterator(1);
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// Output: B C
Removes the element at the specified position in the list and returns it.
xxxxxxxxxx
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
System.out.println(list.remove(1)); // Output: B
System.out.println(list); // Output: [A, C]
Returns the element at the specified position in the list.
xxxxxxxxxx
List<String> list = Arrays.asList("A", "B", "C");
System.out.println(list.get(1)); // Output: B
Replaces the element at the specified position in the list with the specified element.
xxxxxxxxxx
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
list.set(1, "D");
System.out.println(list); // Output: [A, D, C]
Returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.
xxxxxxxxxx
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
List<String> subList = list.subList(1, 3);
System.out.println(subList); // Output: [B, C]
Inserts the specified element at the specified position in the list.
xxxxxxxxxx
List<String> list = new ArrayList<>();
list.add("A");
list.add(0, "B");
System.out.println(list); // Output: [B, A]
[B, A]
Inserts all of the elements in the specified collection into the list at the specified position.
xxxxxxxxxx
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
List<String> anotherList = Arrays.asList("C", "D");
list.addAll(1, anotherList);
System.out.println(list); // Output: [A, C, D, B]
Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.
xxxxxxxxxx
List<String> list = Arrays.asList("A", "B", "C", "B");
System.out.println(list.indexOf("B")); // Output: 1