Essential Java APIs: String Handling, Collections, Dates, and Generics

Core API Concepts Object & String Fundamentals By default, the toString() method in the Object class returns the memory address (hash) of the object, which is often not useful. It is standard practice to override this method to return a string representation of the object's internal state. The == operator compares primitive types by value, ...

Posted on Fri, 15 May 2026 17:35:22 +0000 by m00nz00mer

HashMap Data Structure in Java: Fundamentals and Usage

HashMap vs Hashtable Key Differences Thread Safety: Hashtable is synchronized, while HashMap is not Null Values: HashMap allows one null key and multiple null values; Hashtable prohibits null keys and values Performance: HashMap generally performs better in single-threaded environments due to lack of synchronization overhead HashMap ...

Posted on Thu, 14 May 2026 08:22:06 +0000 by harrylt

Why hashCode Must Be Overridden When equals Is Overridden in Java

Entity Class Definition Let's define a simple Rectangle class that overrides the equals method to compare objects based on their width and height values instead of reference equality. class Rectangle { private int width; private int height; public int getWidth() { return width; } public void setWidth(int width) { ...

Posted on Wed, 13 May 2026 13:15:07 +0000 by nolos

Comprehensive Guide to Java Map Implementations

Map Interface OverviewThe java.util.Map interface defines a structure for storing key-value pairs, where each unique key maps to exactly one value. Key characteristics include:Key Uniqueness: Duplicate keys are not permitted; assigning a new value to an existing key overwrites the old one.Null Handling: Depending on the specific implementation, ...

Posted on Sun, 10 May 2026 20:50:25 +0000 by Steven_belfast

LeetCode Daily Challenge: Convert to 2D Array

Given an integer array nums, construct a 2D array that satisfies the following conditions: The 2D array should contain only elements from the array nums. Each row of the 2D array must consist of distinct itnegers. The number of rows should be minimized. Return any valid result. If multiple solutions exist, any one is acceptable. Note: Rows in ...

Posted on Sun, 10 May 2026 15:14:55 +0000 by songwind

Understanding Hash-Based Collections in Java: HashMap and HashSet Internals

Key-Value vs. Unique-Key Abstractions Java’s Map and Set interfaces serve distinct roles in efficient data retrieval. Map implements a key-value association model—ideal for scenarios like counting word frequencies or mapping identifiers to attributes. In contrast, Set models uniqueness-only lookups—suitable for membership checks, such as verify ...

Posted on Sat, 09 May 2026 09:06:09 +0000 by sigmadog

Implementing Custom Objects as HashMap Keys in Java

In Java, the HashMap implementation uses the hashCode() method to determine the storage bucket for a key and the equals() method to check for key equality. The default implementation in the Object class generates a hash code based on the object's memory address. This behavior creates a problem when using custom objects as keys: two distinct ins ...

Posted on Sat, 09 May 2026 05:35:46 +0000 by elbowgrease

Java Algorithm Patterns: Hash Maps to Array Manipulation

Hash Map Techniques LeetCode 1: Two Sum Utilize a hash map to store elements not yet encountered, while searching for the complement target - nums[i]. public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { in ...

Posted on Fri, 08 May 2026 12:03:41 +0000 by SyWill

HashMap Interview Quick Reference and Best Practices

Core Principles (Three Key Points) Underlying Structure HashMap uses an array as the primary storage, combined with linked lists for handling hash collisions, and converts to red-black trees when certain conditions are met. When a linked list exceeds 8 elements and the array length is at least 64, the structure transforms into a red-black tree, ...

Posted on Fri, 08 May 2026 08:50:05 +0000 by dicky18

How ConcurrentHashMap Achieves Thread Safety in JDK 8

Data Structure The internal structure mirrors HashMap, consisting of a hash table array with linked lists for collision handling. When a bucket accumulates more than eight entries, it transforms into a red-black tree for optimized search performance. Implementation Approach JDK 8 leverages synchronized and CAS operations for concurrent access c ...

Posted on Fri, 08 May 2026 01:24:57 +0000 by FRSH