Category: Java interview questions
-
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
map() and flatmap() methods are found in Optional class and Stream interfaces in Java 8. We’ll discuss the use of both these methods with respect to Optional and Stream. 1. flapMap() and map() methods in Optional 1.1 Syntax of map() <U> Optional<U> map(Function<? super T, ? extends U> mapper) 1.2 Syntax of flatMap() <U> Optional<U>…
-
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…
-
How to Convert a Map to a List in Java?
A Map maps keys to values. Map provides following two methods to return keys and values of a map: public Collection<V> values() This method returns a Collection view of the values contained in this map. public Set keySet() This method returns a Set view of the keys contained in this map Output