Java Collections Framework is a unified architecture for managing groups of objects. It simplifies programming by providing data structures and algorithms for collections, making them easier to use and manipulate.
Java Collections Framework provides a set of interfaces and classes for working with collections.
add()
, remove()
, size()
, iterator()
, etc.ArrayList
, LinkedList
, Vector
HashSet
, LinkedHashSet
, TreeSet
PriorityQueue
, ArrayDeque
HashMap
, LinkedHashMap
, TreeMap
, Hashtable
Collection Type | Characteristics | Common Classes |
---|---|---|
List | Ordered, duplicates allowed | ArrayList , LinkedList , Vector |
Set | No duplicates, unordered | HashSet , LinkedHashSet , TreeSet |
Queue | FIFO or priority order | PriorityQueue , ArrayDeque |
Map | Key-value pairs | HashMap , TreeMap , LinkedHashMap |
add(element)
, addAll(collection)
remove(element)
, clear()
get(index)
get(key)
Iterator
forEach
loopstream().forEach()
)get()
method).add()
/remove()
operations in the middle.HashMap
.HashSet
due to sorting.HashMap
.List<String> list = new ArrayList<>();
Collections.synchronizedList()
or ConcurrentHashMap
.list.stream().filter(e -> e.startsWith("A")).collect(Collectors.toList());
Feature | ArrayList | LinkedList | HashSet | TreeSet |
---|---|---|---|---|
Ordering | Yes | Yes | No | Yes (sorted) |
Duplicates | Yes | Yes | No | No |
Access Speed | Fast (index) | Slow (sequential) | Fast (hash) | Slow (tree-based) |
ConcurrentModificationException
if the collection is modified while iterating.CopyOnWriteArrayList
allow modification during iteration.ArrayList
when frequent random access is needed.HashSet
for unique, unordered collections.ConcurrentHashMap
over Hashtable
.TreeMap
or TreeSet
for sorted data.
Welcome