I used to think equals() and == in Java were basically the same. They’re not. == checks if two references point to the same memory location. equals() checks if two objects are logically equal. That difference looks small. Until your HashMap stops working properly. In Java, if you override equals(), you must also override hashCode(). Because collections like HashMap use hashCode() first to find the bucket, and then equals() to confirm the match. Forget one of them… and your object becomes “invisible” inside the map. One small contract. One big lesson. #Java #BackendDevelopment #SoftwareEngineering #Programming
Java equals() vs ==: A Critical Difference for HashMaps
More Relevant Posts
-
Ever wondered how Java implements HashMap under the hood? I was curious too, so I deep dived into it and built one from scratch. Here's the gist of what's actually happening 👇 - hashCode() → decides WHICH bucket - equals() → finds the exact key inside it - LinkedList → handles collisions (chaining) - Load factor 0.75 → resizes before chains get slow - Java 8+ → chain > 8 nodes? Upgrades to Red-Black Tree The rule most devs forget: Override equals() without hashCode()? Your map silently breaks. Duplicates. Nulls. No errors. Full deep dive + code walkthrough in the article below. https://lnkd.in/gvk9MrqP #Java #DataStructures #SoftwareEngineering #Programming
To view or add a comment, sign in
-
Day 9 – Java Streams Practice Series Today, I worked on finding the first repeated character in a string using Java Streams. Approach: Converted the string into a stream of characters Used Collectors.groupingBy() with LinkedHashMap to preserve insertion order Counted occurrences of each character Filtered characters with frequency greater than 1 Retrieved the first repeated character using findFirst() Key Learning: Using LinkedHashMap is important because it maintains the order of elements, which helps in identifying the first repeated character correctly. Why this matters: This problem helps strengthen concepts like stream transformations, grouping, counting, and order preservation in collections. Consistent practice is helping me build a deeper understanding of Java Streams and problem-solving. #Java #JavaStreams #CodingPractice #DataStructures #ProblemSolving #BackendDevelopment #100DaysOfCode
To view or add a comment, sign in
-
-
Leetcode Problem || Complement of Base 10 Integer(1009) 🚀 The Bitwise Complement problem using Java. 🔹 Idea: The complement of a number means flipping all bits in its binary representation (0 → 1 and 1 → 0). Instead of manually converting the number to binary, I used a bit manipulation trick: 1️⃣ Find the highest set bit using Integer.highestOneBit(n) 2️⃣ Create a mask with all bits set to 1 up to that position 3️⃣ Use XOR (^) with the mask to flip the bits #LeetCode #Java #BitManipulation #CodingPractice #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
#Day17 – Understanding Constructors in Java ⚙️ Today’s session helped me understand how constructors work in Java and how they are used while creating objects. Key Learnings: ✔ A Constructor is a special type of method whose name is the same as the class name ✔ Constructors do not have any return type, not even void ✔ Constructors are automatically called when an object is created using the new keyword ✔ If a programmer does not create any constructor, Java Compiler provides a Default Constructor ✔ Learned about Parameterized Constructors and Zero-Parameterized Constructors ✔ Understood Constructor Overloading (multiple constructors with same name but different parameters) ✔ Explored Constructor Chaining using this(), where one constructor calls another constructor within the same class TAP Academy Harshit T #Java #OOPS #CoreJava #Constructors #Programming #SoftwareDevelopment #LearningJourney #Consistency
To view or add a comment, sign in
-
-
While solving a DSA problem on LeetCode, I noticed the solution used StringBuilder instead of a normal String in Java. At first I thought — why not just use String? 🤔 While exploring this, I learned that every time we modify a String, Java creates a new object in memory. The old objects become unused, which are called garbage values and later cleaned by the garbage collector. A small doubt while solving one question helped me understand how memory actually works in Java 💡 #Java #DSA #LearningInPublic #Programming #LeetCode
To view or add a comment, sign in
-
💻 Java Practice – String Operations Today I continued practicing Java String fundamentals. Worked on small exercises like: • Checking if a string is a palindrome • Counting words in a sentence • Replacing characters using replace() • Comparing strings correctly using equals() Also explored a small but important detail in string comparison and why some approaches are safer in real applications. Consistent practice with small problems helps strengthen programming fundamentals. #Java #ProgrammingFundamentals #LearningInPublic #DeveloperJourney #Consistency
To view or add a comment, sign in
-
Just published a clean and efficient walkthrough of LeetCode 169 – Majority Element using the Boyer–Moore Voting Algorithm in Java. In this breakdown, I explain: 🔹 Why the algorithm works in O(n) time & O(1) space 🔹 How to identify the majority element intuitively 🔹 A clear Java implementation with step‑by‑step reasoning If you're preparing for coding interviews or sharpening your DSA fundamentals, this one’s a must-watch! Watch here: https://lnkd.in/gyB-6DAx Let’s grow together — one problem at a time. 💙💻 #LeetCode #Java #DSA #CodingInterview #ProblemSolving #TechCareer #Algorithms #LearningEveryday
LeetCode 169 | Majority Element | Java Solution Explained Easily (Boyer-Moore Algorithm)
https://www.youtube.com/
To view or add a comment, sign in
-
♻️ Why should you always close Scanner in Java? Scanner is used to read input, from the console, a file, or a stream. But a lot of beginners (including me, early on) never bother closing it after use. Here's why that's a problem: ->it holds onto system resources even after your program is done with them ->for file-based Scanners, it can lock the file or cause data not to be flushed properly ->in larger programs, unclosed Scanners can quietly lead to resource leaks The fix is simple, either call: ✔️ sc.close() at the end ✔️ or use a try-with-resources block so Java closes it automatically While practicing Java basics, I realized the code worked either way… but one way was responsible, and the other wasn't. That's something no compiler warning will tell you. Writing correct code and writing clean, responsible code are two different things. Learning the difference early makes you a better developer. Learning in public, improving step by step 🤍 #Java #ResourceManagement #LearningInPublic #Programming
To view or add a comment, sign in
-
Functions in Java : part 3 - memoization Hi all, in this third part of the serie we 'll have a look on memoization. Hope you will find it useful. Happy coding PS: github repository address on last slide... #java #functionalprogramming #memoization
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development