Help need to know hashcode() and Equals method

Hi ,
Now i'm working under hashtable functions. In that all are asking me to overwrite hashCode and equals method. Why?
Please let me know briefly...........

jverd wrote:
euph wrote:
jverd wrote:
euph wrote:
REFTY5_ wrote:
Hi ,
Now i'm working under hashtable functions. In that all are asking me to overwrite hashCode and equals method. Why?
Please let me know briefly...........Thery're asking you to override the equals and hashCode methods. Override means replace.So does "overwrite." However, as you point out, "override" is the correct word here.So you think "overwrite" means the same as "replace"?To the same extent that "override" does. But both are in the general sense. Override has a specific meaning in this Java context, and overwrite does not.Don't tell me. That's what I said in my original reply.
To my ear it doesn't. To me "overwrite" means the original is destroyed, whereas when something is "replaced" the fate of the original is unknown. So I think there's a slight semantic difference between these words.Fair enough.Fair enougth, but the next time I suggest you're better sorted.

Similar Messages

  • When we override Hashcode and Equal Methods

    Hi....I have doubt regading Hashcode and equal methods
    why we override this two methods....
    why we override hashcode method when we are overriding equal method,
    i would very thankful to give answser
    Thank you
    Ramesh

    hash code is computed to check the equality of two
    objects ,
    that is why if u change the default equal method
    implementation , u need to change the hashcode method
    as well.That's an incomplete answer at best.
    The hashcode method is used by hashing algorithms and data strcutures such as HashMap. This value is used to determine what 'bucket' the reference will go into. It cannot, by itself determine equality.
    If you are still unsure, I suggest looking up 'hash table' on google. I'm sure there's a decent explanation on wikipedia.

  • Why do we need to override Hascode and Equals method?

    Hi,
    == checks if the two references are equal and .equlas will check if the value is same, if we want .equals to take care of both reference and value are correct. why cant I just override equals with an extra check that references are equal(==).
    Please someone elaborate on this and also tell me what role hashcode plays in this.
    thanks
    Anirudh

    anirudh1983 wrote:
    if we want .equals to take care of both reference and value are correct. why cant I just override equals with an extra check that references are equal(==).Many equals() methods do run an '==' check first for efficiency (and I would recommend it if you're writing one yourself).
    Please someone elaborate on this and also tell me what role hashcode plays in this.The reason that it is good practise to override equals() and hashCode() together is to maintain consistency.
    Hashcodes are used by all Java collections that contain the word 'Hash' in their name, and may also be used by other programs that need a hash code for identification; so if you supply one, you must follow the rules (which you can find in the API for Object.equals() and Object.hashCode()).
    The main rule is this: *objects that are equal() must have equal hashcodes*.
    Note that the reverse is NOT true: objects that are not equal() do not have to have different hashcodes, but it is usually better if they do.
    There is quite a lot to know about hashcodes, and what makes a good one, so I suggest you follow dcminter's advice if you want to be a happy and prosperous Java programmer.
    Winston

  • Use of hashCode and equals method in java(Object class)

    What is the use of hashCode and in which scenario it can be used?similarly use of equals method in a class when it overides form Object class. i.e i have seen many scenario the above said method is overridden into the class.so why and in which scenario it has to override?Please help me.

    You could find that out easily with google, that is a standard junior developer interview question.

  • Do I need to override enum's hashCode() and equals()?

    Hi all:
    I have a enum type let's say it is
    public enum Type
      ONE,
      TWO;
    }If I want to compare the enum value to see if they are same value or not, let's say I have two:
    Type a = Type.ONE;
    Type b = Type.TWO;should I use a.equals(b) or (a == b) If I want to use it as another class's key field, and I want to override this class hashCode() and equals() methods.
    Can enum Type return the correctly hash code?
    For example the class is like:
    public Class A
      Type type;
      pubilc boolean equals(Object other)
        // here skip check class type, null and class cast
        if(other.type.equals(this.type))
           return true;
        else
         return false;
      public int hashCode()
        int result = 31;
        result += 31 * result + type.hashCode() ;
       // can we get correct hash code from enum here?
        return result;
    }

    Answered by the test code by myself.
    We can use either equals or (==) to see if they are the same value.
    The enum's hashCode() use System.identityHashCode() to generate the hashCode and they are always correct

  • Method Overriding-toString(),hashCode() and equals()?

    Hi,
    In some Java Classes, I saw that
    toString()
    hashCode()
    equals()
    these 3 methods of Object Class is overloaded.
    I don't understand why and in what situation we have to override these 3 methods of Object Class.
    Thanks in advance for ur Knowledge Sharing.
    Regards,
    S.Prabu

    toString() is typically overridden in subclasses so as to output something useful about a class like instance variable values instead of just a hashvalue representing the object reference as per Object.
    hashCode() and equals() are overridden typically for use within the collections framework though you need to fully understand the hashing contract before starting to override these as they can cause strange results if not overridden correctly.
    Take a look at collections and specifically the hashing functionality of HashMap and HashSet for more info on these.

  • Scenario in which implement or Write our own hascode and equals method

    Dear All,
    Please let me know in which circumstances we write our own Hashcode method and equals method not the default one provided by java.Kindly give me complete code and scenario.
    Thanks
    Sumit

    Thanks for your reply
    I have one more question.
    When equal method of Object class give the same result as equals we override then why should we override equals.For example see the below code
    public class EqualsTest{
    public EqualsTest() {
         super();
    public static void main(String[] args) {
    Moof one =new Moof(8);
    Moof two =new Moof(8);
    if(one.equals(two))
    System.out.println("one is equals to two");
    class Moof {
    private int moofvalue;
    Moof(int val){
    System.out.println("val inside moof constructor"+val);
    moofvalue=val;
    System.out.println("moofvalue inside moof constructor" +moofvalue);
    public int getMoofValue()
    System.out.println("moofvalue inside getmoffvalue method "+moofvalue);
    return moofvalue;
    public boolean equals(Object o)
    System.out.println("Object o in equals"+o);
    if( (o instanceof Moof) &&(( (Moof)o).getMoofValue()==this.moofvalue))
    System.out.println("true");
    return true;
    else
    System.out.println("false");
    return false;
    Here the output we get is :
    val inside moof constructor8
    moofvalue inside moof constructor8
    val inside moof constructor8
    moofvalue inside moof constructor8
    Object o in equalsMoof@119c082
    moofvalue inside getmoffvalue method 8
    true
    one is equals to two
    The last output "one is equals to two" gives the same result as overridng equals by us.So why should we override equals here.
    Thanks
    Sumit

  • My iTunes account has been disabled. I need to know why, and how to get it deactivated.

    My its account has been disabled. I need to know why and how can I get it re-activated? I have a lot of money in that account!

    See if this helps -> http://support.apple.com/kb/TS2446

  • Color management help needed for adobe CS5 and Epson printer 1400-Prints coming out too dark with re

    Color management help needed for adobe CS5 and Epson printer 1400-Prints coming out too dark with reddish cast and loss of detail
    System: Windows 7
    Adobe CS5
    Printer: Epson Stylus Photo 1400
    Paper: Inkjet matte presentation paper with slight luster
    Installed latest patch for Adobe CS5
    Epson driver up to date
    After reading solutions online and trying them for my settings for 2 days I am still unable to print what I am seeing on my screen in Adobe CS5. I calibrated my monitor, but am not sure once calibration is saved if I somehow use this setting in Photoshop’s color management.
    The files I am printing are photographs of dogs with lots of detail  I digitally painted with my Wacom tablet in Photoshop CS5 and then printed with Epson Stylus 1400 on inkjet paper 20lb with slight luster.
    My Printed images lose a lot of the detail & come out way to dark with a reddish cast and loss of detail when I used these settings in the printing window:
    Color Handling: Photoshop manages color, Color management -ICM, OFF no color adjustment.
    When I change to these settings in printer window: Color Handling:  Printer manages color.  Color management- Color Controls, 1.8 Gamma and choose Epson Standard it prints lighter, but with reddish cast and very little detail and this is the best setting I have used so far.
    Based on what I have read on line, I think the issue is mainly to do with what controls are set in the Photoshop Color Settings window and the Epson Printer preferences. I have screen images attached of these windows and would appreciate knowing what you recommend I enter for each choice.
    Also I am confused as to what ICM color management system to use with this printer and CS5:
    What is the best ICM to use with PS CS5 & the Epson 1400 printer? Should I use the same ICM for both?
    Do I embed the ICM I choose into the new files I create? 
    Do I view all files in the CS5 workspace in this default ICM?
    Do I set my monitor setting to the same ICM?
    If new file opens in CS5 workspace and it has a different embedded profile than my workspace, do I convert it?
    Do I set my printer, Monitor and PS CS5 color settings to the same ICM?
    Is using the same ICM for all devices what is called a consistent workflow?
    I appreciate any and all advice that can be sent my way on this complicated issue. Thank you in advance for your time and kind help.

    It may be possible to figure out by watching a Dr.Brown video on the subject of color printing. Adobe tv
    I hope this may help...............

  • ToString array and equals method

    I have an array and i need to get the toString to output the information. How do I get that to happen. This is what i ahve so far.
       public String toString(){
              //should indicate all cities
              System.out.print("Cities :");
              for (int i = 0; i < cities.length -1; i++)
                   return cities;
    Here is another piece of code I am finishing. Please tell me if the loop and equals method is correct but what is the value to return?
       public City getCity(String cityName){
              //returns the City with name cityName (must search for city in the array)
              //if the city does not exist, it returns null
              for(int i = 0; i < cities.length; i++)
                   if(cityName.equals ("cityName"))
                        return ;
                    else return null;
                Message was edited by:
    ehj3000

    great my world class is fixed now im getting missing return statements on the two methods
           public City getCity (String cityName){
          //returns the City with name cityName (must search for city in the array)
          //if the city does not exist, it returns null
             for(int i = 0; i < cities.length; i++)
                if(cities.getName().equals(cityName))
    return cities[i];
    else
    return null;
    public String toString(){
    //should indicate all cities
    System.out.println("Cities: ");
    for (int i = 0; i < cities.length -1; i++)
    return cities[i].getName();

  • I am on a trial version of  InDesign in Creative Cloud and before I purchase need to know why and the files cannot be opened by others using CS6.  Isn't Cloud compatible with CS6 design software?

    I am on a trial version of  InDesign in Creative Cloud and before I purchase need to know why and the files cannot be opened by others using CS6.  Isn't Cloud compatible with CS6 design software?

    Industry standard files such as JPEG are compatible with many brands/versions of programs
    Proprietary files such as those created by InDesign are often not backwards compatible, due to new features in newer program versions
    This is not unique to Adobe... MS Word DOCX files are not backwards compatible with earlier versions of MS Word

  • Help! I know nothing and need to get my info from my Curve onto my Pre

    Ok first off I know nothing so please don't use any techincal terms or assume that I know what an OS something is because I promise I don't.  What I need is to simplely get the info off my Blackberry Desktop Manager onto my new Palm Pre.  I've been at it for days and trying to decipher all the lingo on here and I guess I'm not smarter than a 5th grader because I can't figure it out.
    What I need to know is step by step instructions.  Don't tell me I can save 19.99 if I do certain steps myself because I really just want the easy way out.  Please people have pity on me - I can't even figure out my kids PS3.
    So I don't have anything important in Outlook or any other program than my Blackberry Desktop Manager.  I appreciate any help from those willing to help my lost self.  Thanks a bunch!

    In order to transfer your data to the PRE you will need to have your data in Outlook. Once you have your data in Outlook, you can use the Palm Data Transfer Assistant to transfer your data to the PRE.
    Here is an article on how to setup your Blackberry to sync with Outlook. If you have trouble with this, you will need to call Blackberry support for assistance.
    http://www.ehow.com/how_4502616_sync-outlook-blackberry-via-usb.html
    Once you have the data in Outlook, you can call Palm for setup assistance at the following number, 1-866-750-PALM.
    For reference purposes, click on the following link for the support page for your device on the kb.palm.com webpage.
    http://kb.palm.com/wps/portal/kb/na/pre/p100eww/sprint/home/page_en.html
    There are links on the page to the user guide, troubleshooting, how to's, downloads, etc.

  • SCJP 5.0 A question about hashcode() and equals()

    Here's an exercise from a book.
    Given:
    class SortOf {
    String name;
    int bal;
    String code;
    short rate;
    public int hashCode() {
    return (code.length()*bal);
    public boolean equals(Object o) {
    //insert code here
    Which of the following will fulfill the equals() and hashCode() contracts for this
    class? (Choose all that apply)
    A return ((SortOf)o).bal == this.bal;
    B return ((SortOf)o).code.length() == this.code.length();
    C return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length()*this.bal;
    D return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate ==
    this.code.length()*this.bal*this.rate;
    C is a correct answer but D is also a correct answer according to the book writer,
    and I don't understand why.
    Normally, the rule is that if two objects are equals (according to the equals
    method)) then they must return the same hashcode. If we have two objects A and B;
    and A.bal == 2, A.code.length()==3, A.rate == 4 and B.bal == 4, B.code.length() ==
    3 and B.rate == 2. A and B are equals according with the D answer but they do not
    return the same hashcode.
    May someone help me make it clear?

    Generally ,the instance members decides the equals( ) & hashCode( ) contract . Here the code.length , bal fields are generating the hashCode.
    So it is appropriate to override the equals( ) method in such a way that , thonly those two fields wil decid the equality of the objects .
    Thanks,
    laksh.

  • Need to know schema and table name

    Hi folks,
    I need to know the schema name and the table name associated with a column. Though jdbc has the api to getTableName and getSchemaName, some database vendor like oracle does return empty upon call of mentioned methods. I found that oracle driver does not support that ��
    Can any one give me the solution? It is urgent. Or do you suggest any third pary jdbc driver which can provide those?
    Thanks
    Angelina

    hi,
    does not work for me.
    more pls
    --thanks                                                                                                                                                                                                                   

  • Help needed in redoing networking and home entertainment

    I realize that this is a site dedicated to all things Apple, but I am hoping I can't get some honest and impartial help on building the best home network and entertainment system.
    I currently have an XP Media Center desktop computer in an office of my home where all of the network stuff is at and is connected via ethernet. I also have a Mac G4 MDD 1.0 that I am using in another room for Adobe Creative Suite to do my own promotional items and I have about a 50' Cat 6 cable running back to the router in the office. I also have a Vista laptop that is connected wirelessly but believe it is only a "g" network? I don't really use this a whole lot but take it out to job sites and such so there is information I need to send back and forth to my external drives hooked up via ethernet to my router. I am in the process of getting a new MacBook Pro as well.
    I have a PS3 hooked up to my home entertainment center but unsure of how to make everything work the way that I want. Here is what I would like to be able to do. 1) Networking of all computers and external drives, 2) Operate my Mac G4 Wirelessly, 3) Addition of internal bluetooth to G4 so I can get a wireless keyboard and mouse, 4) Ability to play iTunes from external hard drives on home entertainment center, 3) Ability to stream media from internet and hard drives to HDTV in home entertainment center
    I am looking to upgrade my router to dual band with either the Airport Extreme or the Netgear WNDR3700. I don't know which to get and have found about the same amount of pros and cons for each on the web. Any suggestions and why?
    Do I need an Apple TV or a Netgear WNHDEB111 or other Digital Media Reciever???
    What else would you recommend to accomplish my goals and why do you recommend the part and brand that you do.
    Thank you in advance for any and all assistance I can get.

    Hello, I will gladly assist you with these steps. Have you already set the printer up physically? Are the ink cartridges set in and the printer is ready, just waiting to be downloaded to the computer? If you still have the CD that came with the 8600 printer, all you have to do is place the CD in and click SETUP. This will guide you step by step to install. 
    Now the tricky part is how you want the 2 PC's connected. You can go USB (hardwire from printer to computer), Wireless (set the printer up on network and PC on network with NO cords attached, or you can go Ethernet (a cord going from the printer to the router).
    Let me know how you are able to connect, and I can guide you through those steps.
    **Click the KUDOS star on the left to say 'Thanks'**
    Please mark a reply "ACCEPTED AS SOLUTION" if it solved your problem, so others can find it.

Maybe you are looking for

  • Q info record  bapi/ or FM

    DEAR ALL, Bapi or FM to create / change the Q info record can u update me ? Thanks vraj

  • Help opening a G5 iMac Isight

    I have a G5 iMac Isight (17 inch), whose hard disk has died. Since I have worked on many Mac's, I was going to replace the HD myself. However, I cannot figure out how to open the case. I followed the instructions for the original iMac, but apparently

  • TS3212 Has anyone had trouble downloading and installing itunes 10.7?

    Having trouble configuring my iPhone 5.  Keep getting message that I need itunes 10.7.  Have downloaded it, but when I check for current updates, it keeps telling me that I have the current version...10.6.3.  What am I doing wrong?

  • How do I separate clips into separate events?

    I have two clips merged under one project name. I want to separate them. I have tried to select the bits I want including sound, titles etc but cannot bridge the gap to include the bits I want. What do I do?

  • Reg. InfoSpokes and Open Hub

    Hi What is Open Hub and Info Spoke. And also the difference. regards Sridhar [email protected]