Java Collection Framework

PriorityQueue

The PriorityQueue interface in Java represents a priority queue based on the heap data structure. Unlike a regular queue, where elements are retrieved in a first-in-first-out (FIFO) manner, a priority queue retrieves elements based on their priority. The element with the highest priority is dequeued first. In Java, PriorityQueue is implemented as a priority heap, …

PriorityQueue Read More »

Time and space complexity analysis of different operations.

Analyzing the time and space complexity of operations in various data structures is crucial for understanding their performance characteristics and making informed decisions based on your application’s requirements. 1. Array Operation Time Complexity Access (by index) O(1) Search (unsorted) O(n) Search (sorted, using binary search) O(log n) Insertion/Deletion (at end, if space) O(1) Insertion/Deletion (at …

Time and space complexity analysis of different operations. Read More »

Lambda expressions with collections.

Lambda expressions in Java provide a concise way to express behavior as a method argument, especially when working with collections. They were introduced in Java 8 to facilitate functional-style programming, enabling developers to write cleaner and more readable code. Lambda expressions  provide a concise way to represent anonymous functions — a function without a name …

Lambda expressions with collections. Read More »

Default and static methods in interfaces.

 Java 8 introduced two powerful features in interfaces: Default Methods – These are methods defined in an interface with a body (implementation). They allow you to add new methods to interfaces without breaking the existing implementation classes. Static Methods – These are utility methods that belong to the interface itself, not to instances. Both of …

Default and static methods in interfaces. Read More »

Stream operations and transformations on collections

Stream Operations and Transformations on Collections in Java Java 8 introduced the Stream API, which provides a modern and efficient way to process collections (like List, Set, etc.). Stream operations help to filter, transform, sort, group, and reduce data in a clean and functional style. Two Types of Stream Operations Type Description Intermediate Returns a …

Stream operations and transformations on collections Read More »

Using streams to process collections efficiently.

Using streams in Java allows for efficient and concise processing of collections and other data sources. Streams provide a declarative approach to perform operations on sequences of elements, leveraging functional programming concepts. Operation Traditional Approach Stream API Approach Description Filtering Loop with if condition collection.stream().filter(e -> e.startsWith(“A”)) Filters elements that match a condition. Mapping Loop …

Using streams to process collections efficiently. Read More »

Implementing custom iterators

Implementing custom iterators in Java involves creating classes that implement the Iterator interface or extend classes that provide iterator functionality (Iterator or ListIterator). Custom iterators are useful when you need to define specialized traversal logic or filter elements during iteration. Let’s create an example of a custom iterator for a list of student names. Implementing …

Implementing custom iterators Read More »

Implementing custom collection classes.

Creating custom collection classes in Java involves defining classes that implement the core collection interfaces (List, Set, Map, etc.) or extending existing collection classes to add custom behavior or constraints. Let’s create custom implementations for a List and a Set that store student names, focusing on simplicity and basic functionality. Here’s a simple custom implementation …

Implementing custom collection classes. Read More »

Scroll to Top