Category: Java interview questions
-
How does Stream.peek and forEach differ in behavior
The methods Stream.peek() and Stream.forEach() in Java Streams are superficially similar—they both accept a Consumer and can execute code on each element—but they differ fundamentally in purpose, stream behavior, and best practices. Key Differences Aspect peek() forEach() Type Intermediate operation Terminal operation Stream Continuity Returns a new Stream (can chain more ops) Consumes the Stream (cannot reuse) Typical Intended Use Debugging,…
-
What is the difference between ? vs Object in Java generics
Java generics allow you to write flexible, type-safe code without committing to specific data types. However, when working with collections or generic methods, you might wonder:What is the difference between using ? (wildcard) and using Object as the type parameter? In this tutorial, we’ll break it down clearly with use cases, code samples, and key…
-
Why is Thread.currentThread() a static method in the Thread class?
1. What does Thread.currentThread() do? It returns a reference to the currently executing thread — the thread that is calling this method. If the current thread’s name is “main”, it will print “main”. It tells: “Who is running this code right now?“ 2. Why does it have to be a static method? Because you don’t…
-
Difference between Hashtable and Collections.synchronizedMap
Both provide thread-safe Map implementations, but they differ in several key aspects: Origin and Legacy Synchronization Mechanism Null Keys and Values Iteration Performance Flexibility
-
Why ConcurrentHashMap does not allow null key and value?
The decision to disallow null keys and values in ConcurrentHashMap stems primarily from the desire to avoid ambiguities and potential concurrency issues. Here’s a breakdown: 1. Ambiguity with get(): 2. Concurrency Concerns: 3. Historical Context and Design Decisions: In essence:
-
What is Memoization?
1. Overview Memoization is a technique to store the results of expensive function calls and reuse those results when the same inputs occur again. It avoids redundant calculations, making programs faster and more efficient. Memoization is particularly useful in scenarios involving: 2. How does Memoization work? Memoization works by following these steps: This technique ensures…
-
What is the difference between Map and Object in JavaScript?
In JavaScript, both Map and Object can store key-value pairs, but they have different features, performance characteristics, and use cases. Here’s a detailed comparison: Aspect Object Map Key Types Keys must be strings or symbols (other types are coerced to strings). Keys can be of any type, including objects, functions, and primitives. Order of Keys…
-
Can we make an array volatile in Java?
Before answering this question, let us see two important points about volatile variable. Now, let us answer our original question, Can we make an array volatile in Java? Yes, we can make an array volatile in Java but only the reference variable pointing to the array will be volatile not the elements of the array.…
-
map() vs flatMap() in Java
Overview Both map() and flatMap() are intermediate operations in Java’s Stream API. They are used to transform elements of a stream, but they differ in what they return and how they handle nested structures. 1. map() – Transform Each Element One-to-One Purpose: Method Signature: Characteristics: Example 1: Convert List of Strings to Uppercase Example 2:…
-
Difference between Streams and Collections in Java
A collection is a group of elements. A collection is an in-memory data structure. Collection supports various operations such as sorting, searching, insertion, manipulation and deletion. In Java, collection framework provides many interfaces like Set, List, Queue and implementation classes like ArrayList, HashSet to represent different type of collections. Stream is basically a sequence of…
