[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using objects to index a hastable

Posted on 2004-11-12
6
Medium Priority
?
216 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:cneaton
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
petmagdy earned 500 total points
ID: 12568069
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12568078
You need to override the equals and hashCode methods of Values
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12568095
Sorry - didn't see your comment petmagdy

http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:petmagdy
ID: 12568113
it is ok CEHJ
0
 

Author Comment

by:cneaton
ID: 12568372
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12568384
welcome :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question