Pages

Saturday, August 16, 2025

Transition from C++ to Java (Sheet) helps you switch language faster!

Perfect 😎 I’ll make a visual translation map that you can memorize quickly while coding. I’ll organize it by common operations so you just “see C++ → Java” at a glance.


C++ STL → Java Collections Quick Map

1. Dynamic Arrays

C++ Java Notes
vector<int> v; ArrayList<Integer> list = new ArrayList<>(); Dynamic size
v.push_back(x); list.add(x); Add element at end
v.pop_back(); list.remove(list.size()-1); Remove last element
v[i] list.get(i) Access element
v.size() list.size() Size of array
v[i] = x; list.set(i, x); Modify element

2. 2D Arrays / Lists

C++ Java Notes
vector<vector<int>> mat; List<List<Integer>> mat = new ArrayList<>(); Nested dynamic arrays
mat[i][j] mat.get(i).get(j) Access element
mat[i].push_back(x) mat.get(i).add(x) Append to inner list

3. Stack

C++ Java Notes
stack<int> st; Stack<Integer> st = new Stack<>(); LIFO stack
st.push(x); st.push(x); Add element
st.pop(); st.pop(); Remove top
st.top(); st.peek(); Access top element

4. Queue

C++ Java Notes
queue<int> q; Queue<Integer> q = new LinkedList<>(); FIFO queue
q.push(x); q.offer(x); Add element
q.pop(); q.poll(); Remove front
q.front(); q.peek(); Access front

5. Priority Queue

C++ Java Notes
priority_queue<int> pq; PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); Max-heap
pq.push(x); pq.add(x); Add element
pq.top(); pq.peek(); Max element
pq.pop(); pq.poll(); Remove top

6. Hash Map / Hash Set

C++ Java Notes
unordered_map<int,int> mp; HashMap<Integer,Integer> map = new HashMap<>(); Key-value store
mp[key] = val; map.put(key,val); Insert / update
mp.count(key) map.containsKey(key) Check existence
unordered_set<int> st; HashSet<Integer> set = new HashSet<>(); Unique elements
st.insert(x) set.add(x); Add element
st.erase(x) set.remove(x); Remove element

7. Sorting & Utility

C++ Java Notes
sort(v.begin(), v.end()); Collections.sort(list); Sort ascending
reverse(v.begin(), v.end()); Collections.reverse(list); Reverse order
*min_element(v.begin(), v.end()) Collections.min(list) Minimum
*max_element(v.begin(), v.end()) Collections.max(list) Maximum

8. Other Useful

C++ Java Notes
pair<int,int> p; Map.Entry<Integer,Integer> p; or custom Pair class No native pair in Java
string s; String s; or StringBuilder sb; Strings immutable in Java
bool flag; boolean flag; true / false
for(int x : v) for(int x : list) Enhanced for-loop
v.empty() list.isEmpty() Check empty
const int x = 5; final int x = 5; Immutable variable

💡 Tips to remember:

  • Think push_back → add, pop_back → remove(last), top/front → peek.

  • Vectors → ArrayLists, Sets/Maps mostly same but Java needs class wrappers.

  • Most STL algorithms (sort, reverse, min/max) → Collections methods in Java.


If you want, I can also make a compact “1-page visual map with arrows” showing C++ → Java in a diagram.
It’ll be like cheat-sheet wallpaper for your brain 😎.

Do you want me to do that next?

No comments:

Post a Comment

Multi-dimensional ArrayList in Java

  // import java.util.ArrayList; import java.util. * ; // import java.util.Collections; public class Classroom {     public static voi...