📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Important Key Points About List Interface
Ordered Collection:
Duplicates:
Null Elements:
Methods:
Subinterfaces and Implementations:
ListIterator:
Mutability:
Use-cases:
Equality:
Example 1: List Interface with Its ArrayList Implementation Class
package com.java.collections.interfaces;
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List < String > list = new ArrayList < > ();
// List allows you to add duplicate elements
list.add("element1");
list.add("element1");
list.add("element2");
list.add("element2");
System.out.println(list);
// List allows you to have ‘null’ elements.
list.add(null);
list.add(null);
System.out.println(list);
// insertion order
list.add("element1"); // 0
list.add("element2"); // 1
list.add("element4"); // 2
list.add("element3"); // 3
list.add("element5"); // 4
System.out.println(list);
// access elements from list
System.out.println(list.get(0));
System.out.println(list.get(4));
}
}
[element1, element1, element2, element2]
[element1, element1, element2, element2, null, null]
[element1, element1, element2, element2, null, null, element1, element2, element4, element3, element5]
element1
null
Example 2: List Interface with Its LinkedList Implementation Class
package com.javaguides.collections.linkedlistexamples;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CreateLinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> fruits = new LinkedList<>();
// Adding new elements to the end of the LinkedList using add() method.
fruits.add("Banana");
fruits.add("Apple");
fruits.add("mango");
System.out.println("Initial LinkedList : " + fruits);
// Adding an element at the specified position in the LinkedList
fruits.add(2, "Watermelon");
System.out.println("After add(2, \"D\") : " + fruits);
// Adding an element at the beginning of the LinkedList
fruits.addFirst("Strawberry");
System.out.println("After addFirst(\"Strawberry\") : " + fruits);
// Adding an element at the end of the LinkedList
// (This method is equivalent to the add() method)
fruits.addLast("Orange");
System.out.println("After addLast(\"F\") : " + fruits);
// Adding all the elements from an existing collection to
// the end of the LinkedList
List<String> moreFruits = new ArrayList<>();
moreFruits.add("Grapes");
moreFruits.add("Pyrus");
fruits.addAll(moreFruits);
System.out.println("After addAll(moreFruits) : " + fruits);
}
}
Initial LinkedList : [Banana, Apple, mango]
After add(2, "D") : [Banana, Apple, Watermelon, mango]
After addFirst("Strawberry") : [Strawberry, Banana, Apple, Watermelon, mango]
After addLast("F") : [Strawberry, Banana, Apple, Watermelon, mango, Orange]
After addAll(moreFruits) : [Strawberry, Banana, Apple, Watermelon, mango, Orange, Grapes, Pyrus]
The List Interface Class Diagram
List Interface Methods
List Interface Common Implementation
- ArrayList
- LinkedList
- Vector
- Stack
What's next?
Related Guides
- Collections Framework - The Collection Interface
- Collections Framework - The Set Interface
- Collections Framework - The SortedSet Interface
- Collections Framework - The List Interface
- Collections Framework - The Queue Interface
- Collections Framework - The Deque Interface
- Collections Framework - The Map Interface
- Collections Framework - The SortedMap Interface
Comments
Post a Comment
Leave Comment