Back to all DSA books

Data structures
and algorithms
in Java.

Java for DSA, complete edition
Java for DSA book cover

Learn the algorithms with the collections Java already gives you.

Every example is a complete Java program you run with one command, java File.java: no build tool, no IDE project and no libraries. Each chapter builds the structure by hand, then shows the java.util class that does the same job and what it costs. The book covers the Java details that trip people up in interviews and real code: int overflow, boxed Integer comparisons, equals and hashCode, UTF-16 strings, and a PriorityQueue that prints out of order.

PDF, 71 pages, 1.2 MB. Free with Google sign-in.

Written for Java, not translated into it.

What’s inside.

Each chapter has a complete program to run, the time and memory cost, and exercises.

  1. Install a JDK and prepare your workspace

    Set up Java 25 (or any JDK from 17), a terminal and an editor, and run your first program.

  2. Java foundations for DSA

    Types, arrays, loops and methods, plus the integer overflow and division rules algorithms depend on.

  3. Arrays, lists, maps and sets

    Choose between arrays, ArrayList, HashMap and HashSet, and meet autoboxing.

  4. Strings, chars and code points

    Why length() counts UTF-16 units, and how to build strings without quadratic copying.

  5. Classes, records and references

    Build nodes with classes, data with records, and reason about shared references.

  6. Recursion

    Base cases, smaller problems, and the stack depth that ends in StackOverflowError.

  7. Stacks and queues with ArrayDeque

    Newest first or oldest first, both from ArrayDeque, and why not Stack.

    See stacks and queues visualized
  8. Linked lists

    Build and reverse a singly linked list, and know what java.util.LinkedList really is.

    See linked lists visualized
  9. Hash tables, equals and hashCode

    How a key finds its bucket, what a collision costs, and the contract every key must keep.

    See hash tables visualized
  10. Trees, binary search trees and TreeMap

    Traverse and search a BST, see it degrade, and use TreeMap for guaranteed log n.

    See trees visualized
  11. Heaps and PriorityQueue

    Implement a min-heap, then use PriorityQueue without its ordering surprises.

    See heaps visualized
  12. Searching and sorting

    Binary search without overflow or off-by-one mistakes, and what Arrays.sort guarantees.

  13. Two pointers, sliding windows and prefix sums

    Stop rescanning the same elements, with half-open ranges and long sums.

  14. Divide and conquer and backtracking

    Count inversions by merging, and explore choices while undoing each one cleanly.

  15. Graph traversal with BFS and DFS

    Represent a graph with adjacency lists, visit each vertex once, and recover a shortest path.

    See graph traversal visualized
  16. Greedy algorithms and their limits

    When the locally best choice is globally best, and a counterexample when it is not.

  17. Dynamic programming

    Define a state, write the recurrence, and pick sentinels and types that cannot overflow.

  18. Mixed practice and a small Java project

    Choose a technique without a chapter title to help, and build a dependency planner.

Prefer another language? Go for DSA, JavaScript for DSA, Python for DSA or C++ for DSA.