C++ STL Data Structure | Java Equivalent | Description |
---|---|---|
std::vector |
ArrayList |
Dynamic array that can resize itself. |
std::list |
LinkedList |
Doubly-linked list. |
std::deque |
ArrayDeque |
Double-ended queue (deque) that supports insertion/removal at both ends. |
std::stack |
Stack |
LIFO (Last-In-First-Out) stack. |
std::queue |
Queue (interface) |
FIFO (First-In-First-Out) queue. Commonly implemented with LinkedList . |
std::priority_queue |
PriorityQueue |
Priority queue (heap) where elements are ordered by priority. |
std::set |
TreeSet |
Sorted set implemented as a balanced binary search tree (Red-Black Tree). |
std::multiset |
No direct equivalent | Sorted multiset (allows duplicates). Can be emulated with TreeMap<Integer, Integer> . |
std::map |
TreeMap |
Sorted map implemented as a balanced binary search tree (Red-Black Tree). |
std::multimap |
No direct equivalent | Sorted multimap (allows duplicate keys). Can be emulated with TreeMap<K, List<V>> . |
std::unordered_set |
HashSet |
Hash table-based set. |
std::unordered_map |
HashMap |
Hash table-based map. |
std::unordered_multiset |
No direct equivalent | Hash table-based multiset (allows duplicates). Can be emulated with HashMap<K, Integer> . |
std::unordered_multimap |
No direct equivalent | Hash table-based multimap (allows duplicate keys). Can be emulated with HashMap<K, List<V>> . |
std::pair |
AbstractMap.SimpleEntry |
A pair of two elements. |
- Java Collections Framework: Java provides a rich set of data structures through its Collections Framework, which is similar to C++ STL but with some differences in implementation and naming.
- No Direct Equivalents: Some C++ STL data structures (e.g.,
multiset
,multimap
) do not have direct equivalents in Java. However, they can be emulated using combinations of other data structures. - Thread Safety: Java provides thread-safe versions of some data structures (e.g.,
Vector
,Hashtable
,ConcurrentHashMap
), whereas C++ STL data structures are not thread-safe by default.
- C++:
std::vector<int> v;
- Java:
ArrayList<Integer> list = new ArrayList<>();