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 8397943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:54:28+00:00 2026-06-09T20:54:28+00:00

I have a Java HashMap of type <MyType,Double> . The MyType class has two

  • 0

I have a Java HashMap of type <MyType,Double>. The MyType class has two fields foo (of type String)and bar(of type Double). The equals and hashcode methods for MyType use only foo. Now given an object A of type MyType I need to get the matching entry from the Hashmap. What that means is

MyType A = new MyType();
A.foo = "foo";
A.bar = 0.0;

MyType B = new MyType();
B.foo = "foo";
B.bar = 1.0;

Map<MyType,Double> myMap = new HashMap<MyType,Double>();
myMap.put(B,5.0)

I need to extract the key B(and eventually its bar value) from myMap based on it’s equality with A (since their foo values are same) i.e. a function of the form

Double getBar(MyType type,  Map<MyType,Double> map)

such that

getBar(A,myMap) returns 1.0 

What is the best way to do that? I am not too sure of how this thing is designed in the first place but I am looking for an efficient way of doing this since myMap is expected to be really huge.

UPDATE: A slightly bigger context here is this. I have a set of MyType objects (say S). An external function works on it and creates a HashMap called myMap which calculates and associates a quantity of type Double with each object in the set. It also updates the bar field of each object in the set. What I get back is myMap. Now I need to update each element in my original set S such that each element’s bar value is replaced by what the bar value of the corresponding entry in the returned myMap is. So for each A in S I need to read the corresponding B in myMap, get its bar, and then set the bar of A to be same as bar of B.

  • 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-09T20:54:30+00:00Added an answer on June 9, 2026 at 8:54 pm

    If you do the hashCode and equals based on the foo (which is what you indicate you are doing) then in your example A and B would end up in the same bucket in the hashmap.
    As a result getBar(A,myMap); would return 5.0 because that is what you put in the map- B.
    I mean you can search for B using A but then each put would replace the previous one and I am not sure from your OP what your actual requirement is

    Update:

    Double getBar(MyType type,  Map<MyType,Double> map){  
        if(map.containsKey(type)){   
             for(MyType k:map.keySet()){  
                  if(k.equals(type)){  
                     return k.bar;  
                  }
             }
        }
        return -1.0;
    }
    

    Update 2:
    It seems that what you need is a way to access the keys in your Map directly. You could use do the following:
    Define:

    class Holder{  
        MyType type;  
        Double value;  
    }
    

    and have HashMap<MyType,Holder> instead. So in your method that does the calculation you update the map to add the result in the Holder object where you also store the type. I.e. instead of myMap.put(B,5.0); you do: myMap.put(B,new Holder(B,5.0));

    So you will have:

    Double getBar(MyType type,  Map<MyType,Holder> map){   
         return map.get(type).type.bar;    
    }    
    

    You have extra space requirement for storing the type as part of the value of the map as well but you will have gotten rid of the looping to actually find the type as it is now

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

Sidebar

Related Questions

I have a Java HashMap with Float keys and String values. now given a
So I have a java hashmap like below: hMap.put(1, One); hMap.put(2, Two); hMap.put(3, Two);
So I have two questions about HashMap s in Java: What is the correct
I have wanted to use a HashMap that maps a value of type String
I have a HashMap, its of the type HashMap<String,HashMap<String,int>> now i need to iterate
I have the following class: class IndexItem { private String word; private HashMap<String, Integer>
I have a java class that sets an servlet attribute to a HashMap object:
In my spring application context file, I have something like: <util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String"
I have a HashMap where the key is of type String and the value
I have a Java HashMap containing 5 entries. Mapped to each entry is an

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.