Java Default Collections and LinkedList
Java Default Collections
Java provides a rich Collections Framework in the java.util package, offering pre-built and well-optimised data structures such as:
- List (implementations ArrayList, LinkedList, Vector)
- Set (implementations HashSet, TreeSet, LinkedHashSet)
- Queue/Deque (implementations, ArrayDeque, LinkedList
These collections are designed to handle common programming needs without requiring developers to manually implement data structures.
Advantages of Java Default Collections Library Usage
Efficiency
- Optimised Performance:Â Library collections are implemented by experts and are optimised for speed and memory usage.
- Time-Saving: Developers can use these collections without having to write and debug their own implementations.
The Java ArrayList is a dynamic array that automatically resizes itself, eliminating the need for developers to manually manage array resizing.
Reliability
- Well-Tested: Library collections are thoroughly tested and used by millions of developers, reducing the likelihood of bugs.
- Consistency: They provide consistent behaviour across different projects and platforms.
Flexibility:
The Library offers multiple implementations of the same data structure, allowing developers to select the one that best suits their needs.
In Java, the List interface has multiple implementations like ArrayList, LinkedList, and Vector, each with its own strengths and weaknesses.
Initialisation of Java Collections
When you create a Java collection such as a LinkedList, you must specify the type of elements it will hold using generics:
LinkedList<String> fruits = new LinkedList<>(); // stores Strings
LinkedList<Integer> numbers = new LinkedList<>(); // stores Integers
- The type inside < > defines the kind of elements that can be added to the collection.
- You cannot directly use primitive types (like int, double, char) in generics.
Hence, we need Java Wrapper Classes that convert primitive data types into objects, allowing them to be stored in collections.
| Primitive | Wrapper Class |
|---|---|
| int | Integer |
| long | Long |
| double | Double |
| float | Float |
| char | Character |
| boolean | Boolean |
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(5); // you can still use primitive as wrapper object due to **autoboxing**Common Operations of LinkedList
The standard library provides methods to perform common operations on LinkedLists.
| Operation | Method | Description | Example |
|---|---|---|---|
| Adding elements | add(element) | Adds an element to the end of the list | list.add("apple"); |
| Adding elements | add(index, element) | Inserts an element at a specific position | list.add(1, "banana"); |
| Removing elements | remove(element) | Removes the first occurrence of the element | list.remove("apple"); |
| Removing elements | remove(index) | Removes the element at the specified position | list.remove(0); |
| Accessing and modifying elements | get(index) | Retrieves the element at the specified position | list.set(1, "orange"); |
| Accessing and modifying elements | set(index, element) | Replaces the element at the specified position | list.set(2, "apples"); |
| Checking size and emptiness | size() | Returns the number of elements in the list | list.size(); |
| Checking size and emptiness | isEmpty() | Checks if the list is empty | list.isEmpty(); |
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// initialisation
LinkedList<String> fruits = new LinkedList<>();
// add elements
fruits.add("Apple"); // add at the end
fruits.add("Orange");
fruits.add(1, "Banana"); // insert at index 1
System.out.println("After adding: " + fruits);
// removing elements
fruits.remove("Orange"); // remove by value
fruits.remove(0); // remove by index
System.out.println("After removing: " + fruits);
// add more elements
fruits.add("Orange");
fruits.add("Grapes");
System.out.println("After adding again: " + fruits);
// testing accessing and modifying
String secondFruit = fruits.get(1); // get element at index
System.out.println("Element at index 1: " + secondFruit);
fruits.set(1, "Pineapple"); // replace element at index
System.out.println("After replacing: " + fruits);
// Checking size and emptiness
int size = fruits.size(); // number of elements
System.out.println("Size: " + size);
boolean empty = fruits.isEmpty(); // check if empty
System.out.println("Emptiness: " + empty);
}
}- What are the main advantages of using library collections?
- Why can’t primitive data types (e.g., int, double) be used directly in Java collections, and how can we solve this issue?
- How do you initialise a generic Java collection (e.g., LinkedList<String>) and what does the type inside < > represent?