Skip to main content

DSF

Matrix Programs in Java: Addition, Multiplication, Transpose, Spiral and Rotation

The five classic Java matrix programs on a plain int[][]: addition, multiplication, transpose, spiral order and 90-degree rotation, each with a diagram, a dry run and real JDK 25 output, plus six mistakes that lose marks (an unchanged transpose, a spiral that repeats numbers, a silent int overflow) and operation counts for the complexity question.

LRU Cache in Java: LinkedHashMap, HashMap + Doubly Linked List and a Thread-Safe Version

Three ways to build an LRU cache in Java: LinkedHashMap with accessOrder, HashMap plus a hand-rolled doubly linked list with sentinels, and thread-safe variants (synchronized, ReentrantLock, striping). Includes an O(1) dry-run table, a demo of why ReadWriteLock corrupts the list, a stress test with real assertions, an indicative JMH comparison and Caffeine as the production answer.

Java Collections Framework: Choosing the Right Data Structure

A complete, reference-level guide to the Java Collections Framework. Covers ArrayList vs LinkedList, HashSet vs LinkedHashSet vs TreeSet, HashMap vs LinkedHashMap vs TreeMap, PriorityQueue, ArrayDeque, and immutable collections u2014 with time-complexity tables, annotated code examples, and a decision guide for choosing the right data structure.

Java Streams API: The Complete Reference Guide

A complete, reference-level guide to the Java Streams API (Java 8+). Covers stream creation, intermediate operations (filter, map, flatMap, sorted, distinct, peek), terminal operations (collect, reduce, count, findAny, anyMatch), collectors, parallel streams, and the most common pitfalls u2014 with fully annotated code and sample output for every example.

Implementing Tower of Hanoi Problem in Java

The Tower of Hanoi is a classic mathematical puzzle that elegantly demonstrates the power of recursion. It consists of three rods (pegs) and a number of disks of different sizes that can slide onto any rod. The puzzle begins with all disks stacked in ascending size order on one rod (smallest on top) and the goal is to move the entire stack to another rod. Three rules must be followed: Only one disk may be moved at a time. A disk may only be moved if it is the uppermost disk on its rod. No disk may be placed on top of a smaller disk. The minimum number of moves required to solve the puzzle with n disks is 2n − 1. For 3 disks, that's 7 moves; for 10 disks, 1023 moves.

Implementation of Stack in Java

A stack is a LIFO (Last In, First Out) abstract data type where all insertions and deletions happen at the same end, called the top. Think of a stack of plates: you always add to the top and remove from the top. Stacks are used everywhere in computing — from function call management to expression evaluation and undo functionality. In this post, we implement a stack in Java using a fixed-size integer array, with push, pop, peek, and display operations accessible through a menu-driven console program.