Java List
In Java, a list is an ordered collection of elements. The Java Collections Framework provides several classes and interfaces for working with lists. Here are some of the key classes and interfaces related to lists in Java:
-
List Interface: The
java.util.List
interface is the primary interface for working with lists. It extends theCollection
interface and defines methods for adding, accessing, modifying, and removing elements in a list. Some common methods includeadd
,get
,set
,remove
, andsize
. -
ArrayList: The
java.util.ArrayList
class is a commonly used implementation of theList
interface. It is a dynamic array that can grow or shrink in size as elements are added or removed.ArrayList
provides fast random access to elements but may have slower insertions and deletions in the middle of the list.
List<String> arrayList = new ArrayList<>();
arrayList.add("Item 1");
arrayList.add("Item 2");
arrayList.add("Item 3");
- LinkedList: The
java.util.LinkedList
class is another implementation of theList
interface. It is based on a doubly-linked list data structure.LinkedList
provides fast insertions and deletions in the middle of the list but may have slower random access to elements.
List<String> linkedList = new LinkedList<>();
linkedList.add("Node 1");
linkedList.add("Node 2");
linkedList.add("Node 3");
- Vector: The
java.util.Vector
class is similar toArrayList
but is synchronized, making it thread-safe. However, due to synchronization, it may have lower performance in multi-threaded environments.
List<String> vector = new Vector<>();
vector.add("Element 1");
vector.add("Element 2");
vector.add("Element 3");
- CopyOnWriteArrayList: The
java.util.concurrent.CopyOnWriteArrayList
class is a thread-safe version ofArrayList
. It ensures that the list remains immutable during iteration, making it suitable for scenarios where concurrent reading is common.
List<String> copyOnWriteList = new CopyOnWriteArrayList<>();
copyOnWriteList.add("Value 1");
copyOnWriteList.add("Value 2");
copyOnWriteList.add("Value 3");
- Arrays.asList(): You can convert an array into a list using the
java.util.Arrays.asList()
method.
String[] array = {"A", "B", "C"};
List<String> listFromArray = Arrays.asList(array);
- Immutable Lists: Libraries like Guava and Java 9+ provide immutable list implementations that cannot be modified after creation. These are useful for creating read-only lists.
List<String> immutableList = ImmutableList.of("Apple", "Banana", "Cherry");
When choosing a list implementation in Java, consider your specific requirements, such as the expected size of the list, the type of operations you’ll perform (e.g., random access, insertions, deletions), and whether thread safety is necessary. Each list implementation has its strengths and trade-offs, so choose the one that best suits your use case.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks