# Java for DSA

> Use Java and its Collections Framework for DSA: ArrayList, HashMap, TreeMap, PriorityQueue, ArrayDeque, plus fast input and common boxing traps. Free notes,...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/java-for-dsa
Last updated: 2026-11-28

Use Java and its Collections Framework for DSA: ArrayList, HashMap, TreeMap, PriorityQueue, ArrayDeque, plus fast input and common boxing traps.

Beginner · about 20 hours · difficulty 2/5

Java is a solid choice if your college teaches it or you are targeting Java backend roles. It is verbose but predictable, and the **Collections Framework** covers everything interviews need.

### What to actually learn

- Primitives vs wrappers: `int` vs `Integer`. Collections store objects, so values are **boxed**. Compare Integers with `.equals()`, never `==` (it works only for small cached values, which makes the bug intermittent).
- Core collections: `ArrayList`, `HashMap`/`HashSet`, `TreeMap`/`TreeSet` (sorted, with `floorKey`, `ceilingKey`), `ArrayDeque` (use it for both stack and queue), `PriorityQueue` (min-heap by default).
- Sorting: `Arrays.sort` for primitives, `Collections.sort` or `list.sort((a, b) -> ...)` with comparators. Prefer `Integer.compare(a, b)` over `a - b`, which can overflow.
- Strings are immutable. Build strings in loops with `StringBuilder`, otherwise concatenation becomes O(n^2).
- Fast input: `BufferedReader` plus `StringTokenizer` instead of `Scanner` for large inputs.

### Pitfalls

- `HashMap.get` returns `null` for missing keys; use `getOrDefault` or `merge(key, 1, Integer::sum)`.
- Recursion depth is limited by the thread stack; very deep DFS may need an explicit stack.
- Use `long` for sums of large values.

Good enough to move on: you can write a max-heap with a comparator, a sorted map query like "largest key <= x", and a BFS using ArrayDeque, all from memory.

## Key concepts

- **Primitives, wrappers and autoboxing**
- **ArrayList and arrays**
- **HashMap and HashSet**
- **TreeMap and TreeSet**: floor/ceiling queries
- **ArrayDeque as stack and queue**
- **PriorityQueue with comparators**
- **StringBuilder**
- **Fast I/O with BufferedReader**
- **Generics basics**

## Other options for this step

- [C++ for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpp-for-dsa): Learn the slice of modern C++ that matters for interviews and contests: fast I/O, references, the STL containers and algorithms, and common gotchas.
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/python-for-dsa): Use Python's concise syntax and built-ins (list, dict, set, heapq, deque, bisect, Counter) to solve interview problems quickly, while knowing its performance limits.

## Mini project

Solve 6 warm-ups in Java: word frequency with HashMap.merge, sort a list of intervals by start, top-k frequent elements with PriorityQueue, a sliding maximum with ArrayDeque, 'largest key <= x' queries with TreeMap, and reverse words using StringBuilder.

## Check yourself

Why might comparing two Integer values from a HashMap with == pass small tests but fail on large inputs?

## Free resources

- [The Java Tutorials: Collections](https://docs.oracle.com/javase/tutorial/collections/) (Oracle)
- [The Java Tutorials](https://docs.oracle.com/javase/tutorial/) (Oracle)
- [GeeksforGeeks Java](https://www.geeksforgeeks.org/java/) (GeeksforGeeks)

Previous: [C++ for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpp-for-dsa) · Next: [Python for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/python-for-dsa)

## More in Your Problem-Solving Toolkit

- [C++ for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpp-for-dsa)
- [Java for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/java-for-dsa)
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/python-for-dsa)
- [Complexity Analysis](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/complexity-analysis)
- [Git and GitHub Workflow](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/git-version-control)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
