Category: Java

Posted on: July 2, 2025 Posted by: rahulgite Comments: 0

Java Garbage Collection: Concepts, Algorithms, and Object Lifecycle

1. Reference Variables and Memory Allocation 2. Heap Memory Structure 3. Survivor Spaces and Object Movement Example: 4. Object Metadata Storage 5. Garbage Collectors and Their Algorithms Serial Garbage Collector Concurrency is about dealing with many tasks at once.Parallelism is about doing many tasks at the same time.Concurrency can happen on a single core through context switching, while parallelism requires multiple cores. Parallel Garbage Collector (Throughput GC) Concurrent Mark-Sweep (CMS)…

Posted on: June 26, 2025 Posted by: rahulgite Comments: 0

Lead-Level Java Interview Questions and Answers

1. Performance Optimization of a Java Spring Boot Application Scenario:Worked on a high-traffic e-commerce platform experiencing latency issues and slow API responses during peak sales. Solution Approach: a. Database Optimizations: b. Code-Level Optimizations: c. Infrastructure Scaling: d. Profiling: Results: Example:A product search API that initially fetched product + inventory + reviews in one go was split into micro calls, and non-critical data like reviews was lazy-loaded and cached. 2. Legacy…

Posted on: May 24, 2025 Posted by: rahulgite Comments: 0

Java 21 Features: Before vs After with Examples and Rationale

Java 21, released in September 2023, is a Long-Term Support (LTS) release that finalizes several preview features from Java 17–20 and introduces powerful new capabilities. This document explains each major feature in Java 21 by detailing: 1. String Templates (JEP 430 – Preview) What: Introduces template expressions to simplify and secure string composition. Why: Reduces errors from concatenation and formatting, improves code readability, and supports customizable templates. Before: Now: Benefit:…

Posted on: April 21, 2025 Posted by: rahulgite Comments: 0

Java Scenario Question 1

Given a list of Employee objects, where an employee can appear multiple times due to designation changes, how can we use Java Streams to count the number of unique employees based on their most recent designation? Each Employee object contains: We want to ensure that for each employee, only the most recent record (based on modifiedDate) is considered for counting. Answer: We can solve this using Java Streams by following…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Password Security: Hashing vs Encryption

When storing passwords in a database, hashing is the preferred method over encryption due to its security advantages. Let’s dive into the differences and see examples of both approaches. 1. Hashing the Password Hashing is a one-way function that converts input (password) into a fixed-length string. It is irreversible, meaning you cannot derive the original password from the hash. What is BCrypt? BCrypt stands for Blowfish Crypt. It is a…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Approaches for finding pairs with a sum of 5

Finding Pairs with a Given Sum using HashSet in Java 1. Approach Overview Given an array of integers, the goal is to find all unique pairs where the sum equals 5. We will use a HashSet to efficiently track the elements and their complements. 2. HashSet Approach for Unsorted Input Algorithm Java Code Output Time Complexity Space Complexity 3. Optimized Approach for Sorted Input If the array is already sorted,…

Posted on: March 5, 2025 Posted by: rahulgite Comments: 0

CAS (Compare-And-Swap) Functions in ConcurrentHashMap

ConcurrentHashMap in Java is a thread-safe, high-performance alternative to HashMap that uses Compare-And-Swap (CAS) operations to achieve lock-free updates for certain methods. 1. What is CAS (Compare-And-Swap)? Compare-And-Swap (CAS) is an atomic operation that: Why CAS? 2. CAS-Based Methods in ConcurrentHashMap 1. putIfAbsent(K key, V value) 2. replace(K key, V oldValue, V newValue) 3. compute(K key, BiFunction<K, V, V> remappingFunction) 4. computeIfAbsent(K key, Function<K, V> mappingFunction) 5. computeIfPresent(K key, BiFunction<K,…

Posted on: February 8, 2025 Posted by: rahulgite Comments: 0

Java Concurrent Collections

1. Introduction Java provides several concurrent collections under the java.util.concurrent package, which are designed for multi-threaded environments. These collections are optimized for thread safety and provide better performance compared to traditional synchronized collections. 2. ConcurrentHashMap Functions: Internal Working: 3. CopyOnWriteArrayList Functions: Internal Working: 4. CopyOnWriteArraySet Functions: Internal Working: 5. ConcurrentLinkedQueue Functions: Internal Working: 6. ConcurrentLinkedDeque Functions: Internal Working: 7. BlockingQueue Implementations Types of BlockingQueue: Queue Type Ordering Blocking Behavior ArrayBlockingQueue…

Posted on: February 8, 2025 Posted by: rahulgite Comments: 0

Internal Working of LinkedHashMap in Java

Introduction A LinkedHashMap in Java is a part of the java.util package and is used to store key-value pairs while maintaining insertion order. It extends HashMap and uses a doubly linked list to preserve the order of elements. Unlike HashMap, which does not guarantee any specific iteration order, LinkedHashMap maintains the order in which elements were inserted, making it useful for caching and ordering-sensitive applications. 1. Data Structure Used LinkedHashMap…