Using objects to index a hastable

I've defined an object that holds two strings and a numerical value:

import java.util.*;      

    public class Values{
            public String S1;
            public String S2
            public float N1

      public Values(String s1, String s2, float n1){
            S1 = s1;
             S2 = s2;
            N1 = n1;
      }      
    }


I don't seem to have any problem doing something like the following

Values V = new Values("Experts", "Exchange", 365);
Hash.put(V,"1");

but if I do something like:

Values R = new Values("Experts", "Exchange", 365);
System.out.println((String)Hash.get(R));

I don't get "1" back.  I would like to be able to have R & V access the same position in the Hash.  How can I do that?  Thanks
cneatonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
ur Values must implement equals() and hashCode() something like this:

public boolean equals(object obj)
{
 Values compared = (Values) obj;
  return( S1.equals(compared.S1) && S1.equals(compared.S2) && N1 == compared.N1) ;
}

public int hashCode()
{
 String str = S1 + S2 + String.valueOf(N1);
 return str.hashCode();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You need to override the equals and hashCode methods of Values
0
CEHJCommented:
Sorry - didn't see your comment petmagdy

http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

petmagdyCommented:
it is ok CEHJ
0
cneatonAuthor Commented:
Worked great petmagdy, thanks!  A few changes for anyone else interested in this solution

 Comment from petmagdy  feedback
Date: 11/12/2004 09:30AM PST
 Comment  

//Object should be capped
public boolean equals(Object obj)
{
 Values compared = (Values) obj;

//This should be S2.equals...for the second term
  return( S1.equals(compared.S1) && S2.equals(compared.S2) && N1 == compared.N1) ;
}

public int hashCode()
{
 String str = S1 + S2 + String.valueOf(N1);
 return str.hashCode();
}
 
0
petmagdyCommented:
welcome :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.