
Java Collections – Java List
Java Collections is a framework that is widely used in Java programming to manage and store data efficiently. It provides a set of interfaces and classes that allow developers to manipulate data in various ways. Among the collection types, Java List is one of the most commonly used. In this blog, we will explore the Java List and its various features.
What is a Java List?
A Java List is an ordered collection of elements that can be accessed using an index. It allows the storage of multiple elements of the same type in a single data structure. Lists are dynamic in nature, which means their size can be modified at runtime. The Java List interface extends the Collection interface, which means all the methods available in the Collection interface are also available in the List interface.
Java List Implementation Classes
The Java List interface is implemented by several classes, including:
- ArrayList
- LinkedList
- Vector
- Stack
ArrayList:
The ArrayList class is the most commonly used implementation of the List interface. It provides an array-based implementation of the List interface, which means it uses an array to store the elements. ArrayList provides constant-time access to the elements, which means accessing an element takes the same amount of time regardless of its position in the list. ArrayList is not thread-safe, which means it is not suitable for use in a multi-threaded environment unless proper synchronization is applied.
LinkedList:
The LinkedList class is another implementation of the List interface. It provides a linked-list-based implementation of the List interface, which means it uses a linked list to store the elements. LinkedList provides constant-time insertion and deletion of elements, but accessing an element takes linear time, which means the time taken to access an element depends on its position in the list. LinkedList is also not thread-safe.
Vector:
The Vector class is a legacy implementation of the List interface. It provides an array-based implementation like ArrayList, but it is thread-safe, which means it is suitable for use in a multi-threaded environment. However, Vector is slower than ArrayList due to the overhead of synchronization.
Stack:
The Stack class is also a legacy implementation of the List interface. It provides a last-in-first-out (LIFO) stack data structure. Stack is based on the Vector class, which means it is also thread-safe but slower than ArrayList.
Java List Operations
The Java List interface provides various methods to perform operations on the list. Some of the commonly used methods are:
- add(element): Adds the element to the end of the list.
- add(index, element): Adds the element at the specified index in the list.
- remove(index): Removes the element at the specified index from the list.
- get(index): Returns the element at the specified index in the list.
- set(index, element): Replaces the element at the specified index with the given element.
- size(): Returns the size of the list.
- indexOf(element): Returns the index of the first occurrence of the element in the list.
- contains(element): Returns true if the list contains the specified element, else false.
- isEmpty(): Returns true if the list is empty, else false.
Java List Iteration
Java List provides various methods to iterate over the elements of the list. Some of the commonly used methods are:
- for loop: We can use the
for
loop to iterate over the elements of the list using an index. - forEach loop: We can use the
forEach
loop to iterate over the elements of the list using a lambda expression. - Iterator: We can use the
Iterator
interface to iterate over the elements of the list. The Iterator interface provides methods likehasNext()
andnext()
to iterate over the elements. - ListIterator: We can use the
ListIterator
interface to iterate over the elements of the Java List. - List Iteration section: The list in both forward and backward directions. The
ListIterator
interface provides methods likehasNext()
,hasPrevious()
,next()
, andprevious()
to iterate over the elements.
Example Usage:
Let’s see an example usage of the Java List. Suppose we want to create a list of names and perform various operations on it.
import java.util.ArrayList; import java.util.List; public class ListExample { public static void main(String[] args) { // Create a list of names List<String> names = new ArrayList<>(); // Add elements to the list names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.add("David"); // Print the list System.out.println("Names: " + names); // Add an element at a specific index names.add(2, "Eve"); System.out.println("Names after adding Eve: " + names); // Remove an element at a specific index names.remove(1); System.out.println("Names after removing Bob: " + names); // Get an element at a specific index String name = names.get(2); System.out.println("Name at index 2: " + name); // Replace an element at a specific index names.set(1, "Frank"); System.out.println("Names after replacing Charlie with Frank: " + names); // Iterate over the list using a for loop System.out.println("Names using for loop:"); for(int i=0; i<names.size(); i++) { System.out.println(names.get(i)); } // Iterate over the list using a forEach loop System.out.println("Names using forEach loop:"); names.forEach(name -> System.out.println(name)); // Iterate over the list using an Iterator System.out.println("Names using Iterator:"); Iterator<String> iterator = names.iterator(); while(iterator.hasNext()) { String n = iterator.next(); System.out.println(n); } // Iterate over the list using a ListIterator System.out.println("Names using ListIterator:"); ListIterator<String> listIterator = names.listIterator(); while(listIterator.hasNext()) { String n = listIterator.next(); System.out.println(n); } while(listIterator.hasPrevious()) { String n = listIterator.previous(); System.out.println(n); } } }
Output:
Names: [Alice, Bob, Charlie, David] Names after adding Eve: [Alice, Bob, Eve, Charlie, David] Names after removing Bob: [Alice, Eve, Charlie, David] Name at index 2: Charlie Names after replacing Charlie with Frank: [Alice, Frank, Eve, David] Names using for loop: Alice Frank Eve David Names using forEach loop: Alice Frank Eve David Names using Iterator: Alice Frank Eve David Names using ListIterator: Alice Frank Eve David David Eve Frank Alice
Conclusion
In this blog, we explored the Java List interface and its various implementations. We also saw how to perform various operations on the list and iterate over its elements using different methods. The Java List is a powerful tool for managing collections of elements in a Java program. By understanding its features and methods, developers can efficiently store, manipulate, and access data in a list.
Keywords: Java Collections, Java List, ArrayList, LinkedList, Vector, Stack, Iterator, ListIterator, forEach, for loop, backward iteration, index-based operations, data structure, collections framework, performance optimization, programming best practices.