Collections Framework (C++ STL)

Collections (part of the java.util package like ArrayList, HashSet, HashMap) solve this by providing dynamic data structures that can grow and shrink automatically.
They also come with built-in algorithms for searching, sorting, manipulating data, and handling types safely via generics.
The reference is created on stack and object is stored on heap.

List

ArrayList (Not threadsafe) (C++ std::vector)

Array is Java. It is fast for random access and appends, but slower for insertions/removals in the middle.


List<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
System.out.println(nums.get(1));
      

LinkedList (C++ std::list)

LinkedList stores elements as nodes, so insertions and removals are efficient, but random access is slower.


List<String> links = new LinkedList<>();
links.add("one");
links.add("two");
links.remove(0);
      

Vector(threadsafe) (C++ std::vector)

Vector is similar to ArrayList but is synchronized, making it thread-safe at the cost of extra overhead.


Vector<String> v = new Vector<>();
v.add("safe");
      

Stack (C++ std::stack)

Stack is a legacy LIFO structure. Use it when you need push/pop semantics.


Stack<String> st = new Stack<>();
st.push("first");
st.push("second");
System.out.println(st.pop());
      

Set (C++ std::set)

Set stores unique elements only. Duplicate inserts are ignored.

HashSet (C++ std::unordered_set)

HashSet stores unique values using hashing and does not preserve order.


Set<String> set = new HashSet<>();
set.add("one");
set.add("one");
System.out.println(set.size());
      

LinkedHashSet (HashSet with order)

LinkedHashSet keeps the insertion order while still preventing duplicates.


Set<String> ordered = new LinkedHashSet<>();
ordered.add("b");
ordered.add("a");
      

TreeSet

TreeSet stores unique values in sorted order.


Set<Integer> sorted = new TreeSet<>();
sorted.add(10);
sorted.add(5);
      

Map

Map stores key-value pairs. Keys must be unique, but values can be duplicated.

HashMap (C++ std::unordered_map)

HashMap stores key-value pairs with fast lookup. It does not preserve insertion order and allows one null key.


Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
System.out.println(map.get(1));
      

LinkedHashMap

LinkedHashMap behaves like HashMap but preserves insertion order.
C++ comparison: ordered hash map; closest is a custom wrapper over a hash map.


Map<String, Integer> orderMap = new LinkedHashMap<>();
orderMap.put("A", 1);
orderMap.put("B", 2);
      

TreeMap (C++ std::map)

TreeMap stores entries in sorted key order.


Map<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("z", 26);
sortedMap.put("a", 1);
      

Hashtable (Synchronized std::unordered_map)

Hashtable is a synchronized version of HashMap, similar in purpose but older and less flexible.


Hashtable<String, Integer> table = new Hashtable<>();
table.put("x", 10);
      

Queue and Deque (std::queue, std::deque)

Queue supports FIFO operations, while Deque supports both FIFO and LIFO behavior.

Queue / PriorityQueue (std::priority_queue)

Queue uses FIFO order, while PriorityQueue orders elements by priority.


Queue<String> q = new LinkedList<>();
q.add("first");
q.add("second");
System.out.println(q.remove());

PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq.peek());
      

Deque / ArrayDeque (std::deque)

Deque is a double-ended queue. It can be used as a stack or queue.


Deque<String> dq = new ArrayDeque<>();
dq.addFirst("A");
dq.addLast("B");