Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:07:04+00:00 2026-06-06T11:07:04+00:00

I have the following situation. I have a HashMap in Java with keys as

  • 0

I have the following situation. I have a HashMap in Java with keys as strings.
Then in some stage , in runtime I create strings equal to those keys in order to retrieve the data from that map.The strings are created as follows within “for” loop:

 String keyToRetrive = "lights[" + Integer.toString(i) + "]" + ".Intensity";

The strange thing about it that when I iterate through the map to find the key that equals that string ,even when the match is found the search steps over.So in this search loop :

  while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        if (name == entry.getKey()) {  ///name- "lights[0].Intesity"
            uniformOut = (ICleanable) entry.getValue();
            break;
        }
    }

The key with the name “lights[0].Intesity” never returns true even that the map contains one.How I solved it .I used hashCode() for both compared string values.So this version does work:

 while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        if (name.hashCode() == entry.getKey().hashCode()) {
            uniformOut = (ICleanable) entry.getValue();
            break;
        }
    }

UPDATE: After being pointed to the fact that “==” doesn’t work good and “equals()” should be used I would like to narrow the question:Why “==” does work for strings which were not created from several concatenated blocks? I mean, if I defines key string to compare agains as a simple single string:

 String foo="foo";

Such a string is compared using “==” against HashMap key all right.

I am not an expert Java programmer so can anybody explain why it works this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:07:05+00:00Added an answer on June 6, 2026 at 11:07 am

    You are comparing Strings using == operator. Use equals() instead:

    name.equals(entry.getKey())
    

    This is a common pitfall in Java, see How do I compare strings in Java? and Difference between Equals/equals and == operator?.


    BTW (unrelated to your problem) when concatenating strings you don’t need to call toString() explicitly so this:

    "lights[" + Integer.toString(i) + "]" + ".Intensity"
    

    can be replaced with:

    "lights[" + i + "]" + ".Intensity"
    

    It’ll work for i of any type, not only int.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Situation, Server A sends some data (HTML form) to server B,
I have the following situation and I'm expecting some expert advise from SO folks.
I have the following situation: // A public interface of some kind public interface
For some reason, I have following situation: Now I want to get rid of
I have the following situation: using a classical Java server (using ServerSocket) I would
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I have following situation: String a = A Web crawler is a computer program
I have the following situation. Im getting data from an external system in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.