Category Archives: DSF

Java Collections Framework: Choosing the Right Data Structure

Java ships with a rich library of data structures under the java.util package. The problem is not finding a collection u2014 it is picking the right one. Using an ArrayList when you need fast membership checks, or a HashMap when you need sorted keys, is the kind of mismatch that shows up in code reviews and performance profiles.

This guide maps every important class to the use case it is designed for, explains the time complexity of its key operations, and provides working code you can copy directly into a project. By the end you will have an intuition for which collection to reach for in any situation.

Continue reading Java Collections Framework: Choosing the Right Data Structure

Java Streams API: The Complete Reference Guide

The Streams API, introduced in Java 8, fundamentally changed how Java developers process collections. Instead of writing loops that describe how to iterate, you write pipelines that describe what to compute. The result is code that is shorter, easier to parallelise, and far more composable than the equivalent imperative loop.

This guide is designed as a reference you will return to. Every method in the API is covered with a working example and annotated output. The final sections address collectors in depth, parallel streams, and the mistakes that trip up developers who are new to the functional style.

Continue reading Java Streams API: The Complete Reference Guide

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:

  1. Only one disk may be moved at a time.
  2. A disk may only be moved if it is the uppermost disk on its rod.
  3. 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.

Continue reading Implementing Tower of Hanoi Problem in Java

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.

Continue reading Implementation of Stack in Java

Implementing Singly Linked List in Java

A singly linked list is a linear data structure made up of nodes, where each node contains a data field and a pointer to the next node in the sequence. Unlike arrays, linked lists do not require contiguous memory — elements can be inserted or removed at any position without shifting other elements.

In this post, we implement a full-featured singly linked list in Java that supports insertion (at first, at last, at a given position), deletion (from first, last, or a given position), search, and display.

Singly Linked List
A singly linked list — each node stores a value and a link to the next node.
Continue reading Implementing Singly Linked List in Java

Java Implementation of Queue using Linked List

A queue is a FIFO (First In, First Out) data structure where elements are added at the rear and removed from the front. While a queue can be implemented using a fixed-size array, backing it with a linked list removes the capacity constraint — the queue grows and shrinks dynamically as elements are enqueued and dequeued.

In this post, we implement a queue using a singly linked list in Java, supporting enqueue, dequeue, peek, and display operations.

Continue reading Java Implementation of Queue using Linked List

Implementing Quick Sort in Java

Quick Sort is a highly efficient, comparison-based sorting algorithm that uses the divide-and-conquer strategy. It selects a pivot element, partitions the array into elements smaller than the pivot and elements larger than the pivot, then recursively sorts each partition. Its average-case time complexity is O(n log n), making it one of the fastest general-purpose sorting algorithms in practice.

Continue reading Implementing Quick Sort in Java