Java Collection Classes
The Java Collections Framework consists of many classes that implement the Collection interface, each designed for specific use cases and characteristics.
Let’s discuss few of them :
1. ArrayList
ArrayList provides resizable-array implementation of the List interface. It allows for dynamic arrays that can grow as needed.
Syntax:
List<String> list = new ArrayList<>();
2. Vector
Similar to ArrayList, Vector implements a dynamic array that can grow or shrink as required. However, unlike ArrayList, it is synchronized, making it thread-safe.
Syntax:
Vector<Integer> vector = new Vector<>();
3. Stack
Stack class represents a last-in-first-out (LIFO) stack of objects. It extends Vector with five operations that allow a vector to be treated as a stack.
Syntax:
Stack<Double> stack = new Stack<>();
4. LinkedList
LinkedList implements both List and Deque interfaces, allowing it to be used both as a list and as a queue. It is ideal for applications with frequent insertions and deletions.
Syntax:
LinkedList<Character> linkedList = new LinkedList<>();
5. HashSet
HashSet is used to create a collection that uses a hash table for storage. It exemplifies the Set interface, guaranteeing unique elements.
Syntax:
Set<Long> hashSet = new HashSet<>();
6. LinkedHashSet
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.
Syntax:
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
7. TreeSet
TreeSet is a NavigableSet implementation that uses a Red-Black tree architecture. It orders its elements based on their values.
Syntax:
TreeSet<String> treeSet = new TreeSet<>();
8. PriorityQueue
PriorityQueue class is a priority queue based on the priority heap. Elements of the priority queue are ordered according to their natural ordering.
Syntax:
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
9. ArrayDeque
ArrayDeque class provides a way to apply resizable-array implementation of the Deque interface. It is faster than Stack and LinkedList as a stack and queue.
Syntax:
ArrayDeque<String> arrayDeque = new ArrayDeque<>();
10. HashMap
HashMap is a part of Java’s collection framework and provides the basic implementation of the Map interface of Java. It stores data in key-value pairs.
Syntax:
HashMap<String, Integer> hashMap = new HashMap<>();
11. EnumMap
EnumMap is a specialized Map implementation for use with enum keys. It is compact and efficient.
Syntax:
EnumMap<MyEnum, String> enumMap = new EnumMap<>(MyEnum.class);
12. AbstractMap
This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
Syntax:
AbstractMap<String, Integer> abstractMap = new HashMap<>();
13. TreeMap
TreeMap is a map implemented by a Red-Black tree. It stores keys in a sorted order, and it is part of the Java Collections Framework.
Syntax:
TreeMap<String, Integer> treeMap = new TreeMap<>();
Java Collections Class Fields
When working with the Java Collections Framework, it's important to understand the typical fields associated with these classes, which help in managing the collection's state and behavior. Let’s see some of the common fields found in collection classes:
1. Capacity- This field typically applies to classes like ArrayList and Vector, where it refers to the size of the array used to store the elements in the list. It adjusts automatically as elements are added or removed.
2. Size- The size field indicates the number of elements currently stored in the collection. This is applicable across all collection types.
3. Comparator- Used in sorted collections like TreeSet and TreeMap, this field defines a comparator that is used to order the elements in the collection.
4. Load Factor- Found in hash table based collections like HashSet and HashMap, the load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
5. Mod Count- This field is used internally in many collections to ensure the integrity of iterators. It counts the number of times a collection has been structurally modified.
Java Collections Examples
Adding Elements to the Collections
ArrayList Example:
List<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Rinki");
names.add("Harsh");
System.out.println("Names List: " + names);

You can also try this code with Online Java Compiler
Run CodeOutput:
Names List: [Rahul, Rinki, Harsh]
Sorting a Collection
TreeSet Example:
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);
System.out.println("Sorted Numbers: " + numbers);

You can also try this code with Online Java Compiler
Run CodeOutput:
Sorted Numbers: [1, 3, 5]
Searching in a Collection:
HashSet Example
Set<String> items = new HashSet<>();
items.add("pen");
items.add("notebook");
items.add("eraser");
boolean hasNotebook = items.contains("notebook");
System.out.println("Contains notebook: " + hasNotebook);

You can also try this code with Online Java Compiler
Run CodeOutput:
Contains notebook: true
Copying Elements
Copying from one List to another:
List<Integer> original = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> copy = new ArrayList<>(original);
System.out.println("Copied List: " + copy);

You can also try this code with Online Java Compiler
Run CodeOutput
Copied List: [1, 2, 3]
Disjoint Collection
Check if two collections have no elements in common:
List<String> group1 = Arrays.asList("apple", "banana", "cherry");
List<String> group2 = Arrays.asList("dog", "cat", "bird");
boolean isDisjoint = Collections.disjoint(group1, group2);
System.out.println("Collections are disjoint: " + isDisjoint);

You can also try this code with Online Java Compiler
Run CodeOutput
Collections are disjoint: true
Frequently Asked Questions
What is the difference between ArrayList and LinkedList in Java?
ArrayList is implemented as a dynamic array, while LinkedList is a doubly-linked list. ArrayList provides faster access to elements, but LinkedList offers better performance for insertion and deletion operations, especially at the beginning or middle of the list.
Can we use primitive data types with Java Collections?
No, Java Collections only work with object types. However, you can use wrapper classes like Integer, Double, or Boolean to store primitive values in collections.
What is the purpose of the Collections.synchronizedCollection() method?
The Collections.synchronizedCollection() method is used to create a thread-safe collection by wrapping the specified collection with a synchronized wrapper. This allows multiple threads to safely access the collection concurrently.
Conclusion
In this article, we have learned about the Java Collections class and its various components. We explained different collection classes like ArrayList, Vector, Stack, LinkedList, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, HashMap, EnumMap, AbstractMap, and TreeMap. We also looked at the syntax and examples for each of these classes.