4

My problem is I have a HashMap where the key is an object and I need it to be one of the variables in said object. Currently, the HashMap.get() only looks at the myNums Objects but I need a way to get the value of num1 as thats the "real key". I can't use the myNums.get() methods because I don't have an instantiation of myNums. I coud loop through each item in the HashMap to check but I'd prefer not to do that. Is there a more elegant solution?

What I have:

public static void main(String [] args){
   int [] array = {//integers 1-100};
   HashMap < myNums, String > hash = //data from another source;
   for(int i = 0;i < array.length; i++){
       if(hash.get(i) != null)
          OtherFunction(hash.get(i));         
   }
}

public class myNums{
   private int num1;
   private int num2;
   //get and set functions...
}
6
  • 4
    Why are you defining this HashMap using the Object if the real key is an int? Why not HashMap<Integer, String>? Or better, Use an ArrayList - docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html Aug 9, 2012 at 21:25
  • For all intents and purposes, lets say I agree with you, but the decision is out of my hands
    – Daniel
    Aug 9, 2012 at 21:27
  • @FrankieTheKneeMan In the case that the key is not confined to a small range, it might be better to use a Map over a RandomAccess List.
    – obataku
    Aug 9, 2012 at 21:28
  • 1
    @FrankieTheKneeMan It seems that he might be creating a multi-key map, where there are multiple int keys for each String value.
    – FThompson
    Aug 9, 2012 at 21:28
  • 1
    @veer Multimaps are multiple values associated with each key, not multiple keys for each value.
    – FThompson
    Aug 9, 2012 at 21:30

2 Answers 2

1

Maybe an entry iteration serves more your purpose:

for (Entry<MyNums, String> entry : map.entrySet()) {
    System.out.println("Key:" + entry.getKey());
    System.out.println("Value:" + entry.getValue());
}

With this iteration style you don't have to invoke get all the time.

2
  • wont I still have to loop through the entire Set to check if its there? In this case it would be a small set so its not really an issue, but hypothetically if it were huge...
    – Daniel
    Aug 9, 2012 at 21:44
  • @Daniel Yes, that won't help then, but it's still better then iterating the whole collection and calling get in each step. Aug 9, 2012 at 21:49
0

You should override the hashCode() method and extend the equals() method of your class.

private class myNums{
  public int hashCode(){
    //Return something unique
  }
  public boolean equals(myNums that){
    return this.num1==that.num1;
  }
}
4
  • You can not overridden equals but overloaded it.
    – Steve Kuo
    Aug 9, 2012 at 21:57
  • Thank you, @platzhirsch. Overloading is when you have two methods with different parameters but the same name. Aug 9, 2012 at 21:59
  • Correct. In your case you really want to implement equals(Object obj) and not equals(myNums obj).
    – Steve Kuo
    Aug 9, 2012 at 22:15
  • Oh, shit, you're right! But really, you only need to implement myNums in this situation. You can let the Object class return null for you. Aug 9, 2012 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.