Posts

Showing posts with the label DevForgeHub

Serialization, Deserialization and Externalization in Java

Image
In Java interviews, one of the most common topics you’ll encounter is object persistence —how Java objects are saved, transferred, and reconstructed. This is where concepts like Serialization , Deserialization , and Externalization come into play. These aren’t just theoretical ideas; they are crucial in distributed systems, caching, session management, and inter-process communication . Let’s explore these concepts step by step, highlight their differences, and discuss real-world use cases. 1. What is Serialization in Java? Serialization is the process of converting a Java object into a byte stream so that it can be stored in a file, transmitted over a network, or persisted in a database. How it works: The class must implement the Serializable interface (marker interface). Use ObjectOutputStream.writeObject() to serialize an object. Non-transient, non-static fields are serialized; transient fields are skipped. ...

Internal Working of Map & HashMap in Java (Interview Ready)

Image
🔹 Introduction In the Java Collections Framework, both Map and HashMap are essential for working with key-value pairs. This blog dives into their internal mechanics, including how objects are stored and retrieved, how collisions are managed, and what improvements Java 8 brought to the table. 1. What is a Map? A Map<K, V> is a data structure that stores unique keys associated with corresponding values. Common implementations include: HashMap – unordered, allows one null key and multiple null values TreeMap – sorted by keys LinkedHashMap – maintains insertion order 2. How HashMap Works Internally ➡️ Nodes & Buckets Internally, a HashMap uses an array of Node objects , where each node represents a key-value pair plus a pointer for chaining in case of collisions. class Node<K,V> { int hash; K key; V value; Node<K,V> next; } ➡️ Hashing & Index Calculation ...

Understanding Multiple Threads in Java

Image
Introduction In Java programming, achieving parallel execution of tasks is made possible using multithreading. This blog explores how to create and run multiple threads concurrently, why using start() is critical, how scheduling works, and additional tips like using sleep() for clearer outputs. Demonstrating Multiple Threads Let's start with a simple example. We define two threads—one to say “Hi” and another to say “Hello”. class HiThread extends Thread {   public void run() {     for(int i = 0; i < 5; i++) {       System.out.println("Hi");       try { Thread.sleep(500); } catch(Exception e) {}     }   } } class HelloThread extends Thread {   public void run() {     for(int i = 0; i < 5; i++) {       System.out.println("Hello");       try { Thread.sleep...

Threads in Java

Image
Introduction In modern applications, responsiveness and efficiency are essential. Whether you’re building a desktop app, a game, or a backend server, you often need to perform multiple tasks simultaneously. This is where Threads in Java come into play. Threads allow you to run multiple tasks concurrently, making programs faster and more efficient. In this blog, we’ll explore: What threads are and why they are important How to create and manage threads in Java The thread lifecycle Common problems like race conditions How to ensure thread safety What is a Thread? A thread is the smallest unit of execution in a program. While a process represents an entire running application, threads are lightweight sub-tasks inside that process. 👉 Example: When you use WhatsApp, one thread handles message sending, another handles receiving, while another manages notifications — all at the same time. This is possible because of multithreading , which enables multiple threa...

Java Collection Framework Hierarchy

Image
The Java Collection Framework (JCF) is a unified architecture of interfaces , implementations (classes), and algorithms to store and process groups of objects. This guide explains the full hierarchy: Collection (List/Set/Queue/Deque), Map , their sorted/navigable variants, core implementations, iterators, and key concurrent & legacy types. Block Diagram (Hierarchy Overview) Iterable │ (iterator()) │ Collection ┌───────────────────┼───────────────────┐ │ │ │ List Set Queue │ │ │ ┌─────┼─────┐ ┌──────┼──────┐ ┌──┴────────┐ ArrayList LinkedList HashSet LinkedHashSet PriorityQueue Deque Vector │ │ │ │ │ also...

Java Streams Collector Operations Explained with Examples

Image
Collectors are the workhorses of Stream terminal aggregation in Java 8. They let you join strings, group and partition data, build specific collection types, and compute statistics — all in a fluent, readable style. 1) Collectors.joining() — Concatenate elements Concatenate stream elements into a single string with optional delimiter, prefix, and suffix. List<String> words = Arrays.asList("apple", "banana", "cherry"); String joined = words.stream() .collect(Collectors.joining(", ", "[", "]")); System.out.println(joined); // [apple, banana, cherry] 2) Collectors.groupingBy() — Group by classifier Group elements based on a key extractor. Default downstream is toList() . List<Person> people = Arrays.asList( new Person("John", "New York"), new Person("Wick", "London"), new Person("Smith", "New York") ); Map...

Java Streams Terminal Operations Explained with Examples

Image
In our previous post, we explored intermediate operations in Java Streams. Now, let’s move to terminal operations . These are the final operations that produce a result (like a value, collection, or side-effect) and terminate the stream pipeline. 1. forEach() The forEach() method is used to perform an action for each element of the stream. List<String> words = Arrays.asList("apple", "banana", "cherry"); words.forEach(word -> System.out.println("item is " + word)); 2. collect() The collect() method gathers the elements of a stream into a collection (like a List, Set, or Map). List<Integer> nums = Arrays.asList(1,1,2,2,2,3,4,5,5,5,6); Set<Integer> res = nums.stream().collect(Collectors.toSet()); System.out.println(res); 3. reduce() The reduce() method performs a reduction on the elements of the stream using an accumulation function. List<Integer> nums = Arrays.asList(1,2,3,4,5); Optional<Integer...