Over ride the equal method in java

Hi ,
I have doubt about on over ride the equal ? What is the main purpose of over ride the equal method ?
If equal method is override then why hash code method can be over ride?
sorry , If i am not post the right place .

You have to override the equals method when your class has a notion of logical equality, because in that case you need more that the default one (the default one return true only if the reference of the compared Object is the same).
A good exemple is Date, where we consider that is two Dates have the same getTime() they are equals.
You can found more information in this article:
http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf
Edited by: yannig on 16 déc. 2010 03:42: Joachim beat me...
Edited by: yannig on 16 déc. 2010 03:45

Similar Messages

  • Over-riding the paint() method

    I want to create a custom button-rounded sides with straight top and bottom to be exact. I understand that you can use the Component class to create components, and over-ride the paint() method to create the actual button's appearance, but how do you do this? Also how do you go about creating an actionlistener for a component like my button idea? Any help will be appreciated!

    Since you are creating your own JButton you want to implement MouseListener so that you can detect the mouse up and mouse down events. If you are going to have a hover image then you want to implement MouseMotionListener as well. Here is an example:
    public class MyButton extends JComponent implement MouseListener, MouseMotionListener {
      private Vector listeners = new Vector();
      public MyButton() {
        addMouseListener(this);
        addMouseMotionListener(this);
      public void addActionListener(ActionListener al) {
        listeners.add(al);
      public void removeActionListener(ActionListener al) {
        listeners.remove(al);
      //add the overridden methods from MouseListener, MouseMotionListener
      public void paint(Graphics g) {
        //paint based on the events
    }This class will be notified when the mouse enters, exits, is clicked, etc. and you can set a state variable so that your paint routine knows what image to paint.
    Hopefully this will clear it up. Not sure how I can make it clearer.

  • How can I over-ride the regional code?

    I buy DVD from different countries.
    In my Microsoft I could over-ride the regional code.
    Question: What can I do with my Probook to play DVDs from variious regional codes?
    Is there a software avaliable or what else can I do?
    Volker

    "Is there a software avaliable or what else can I do?"
    There is no method by which an MBP can have the regional code reset.  There are external players that do not utilize the regional coding, and allow all optical media to be used.  Google is your friend.

  • What would be the result if we overrode the equals method properly

    import java.util.*;
    class Nearly{
       String value:
       Nearly(String v){ value = v;}
       public int hashCode(){ return 1;}
       public boolean equals(Nearly n){
       if(value.charAt(0) == n.value.charAt(0)) return true;
       return false;
       public static void mian(String [] sss){
         Nearly n1 = new Nearly("aaa");
         Nearly n2 = new Nearly("aaa");
         String s = "_";
         if(n1.equals(n2)) s += "1";
         if(n1==n2) s += "2";
         Set <Nearly> set = new HashSet <Nearly>();
          set.add(n1);
          set.add(n2);
         System.out.println(s + " " + set.size());
    }Ans: b) -1 2
    Reason: Two different equals() methods are invoked, because the equals() method shown in the code doesn't properly override Object.equals(), which takes an Object
    Ok so what would have been the result if the equals() method was something like this:
    public boolean equals(Object n)
    {I guess my question is about the overriding the equals() method which i am not able to understand

    pksingh79 wrote:
    what would be the result if we overrode the equals method properlyThe result would be a method, the functionality of which would adhere to Object#equals()'s contract:
    public boolean equals(Object obj)
    Indicates whether some other object is "equal to" this one.
    The equals method implements an equivalence relation on non-null object references:
    &#42; It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    &#42; It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and
    only if y.equals(x) returns true.
    &#42; It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and
    y.equals(z) returns true, then x.equals(z) should return true.
    &#42; It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y)
    consistently return true or consistently return false, provided no information used in equals
    comparisons on the objects is modified.
    &#42; For any non-null reference value x, x.equals(null) should return false.

  • Can I over-ride the default setting in Organizer to allow sorting photos by filename ?

    I am converting slides from the 1970's and 80's to digital files and using PSE 8 to Organize them but it is important to me that they stay in the order of the file name I give them and NOT "Date: newest first or last" because it would seem that PSE, in the absence of metafile information, uses the date and time I uploaded the file to the Organizer as the "Date:".  ACDSee has a much more sensible arrangement in giving me full control of how my photos are sorted.  I am being driven to using ACDSee as the organizer and PSE as the photo editor, a not very convenient arrangement.  So my question is, "Can I over-ride the default setting in Organizer to allow sorting photos by filename ?"

    The type of file system that you're trying to set up, is actually done better by creating you own file heirarchy in Windows, and then viewing by import batch.  For example; you might create a top level folder "My Slide Scans", then perhaps, "My son John". So, the scans of john all go in the same folder, you import the folder in one batch, and you're where you want to be.
    That said, PSE will fight you at every turn to do this, since with the scanner hooked up with PSE, PSE is going to want to store your photos in "scanned photos" and import them as you scan them.
    I woud suggest this.  After the scan and import, use PSE to move the photos to their final destination folder, say " Hawaii 1994", and create a tag of the same name". Then when you're done scanning photos of this event, use the tag to view all the scans at once. Then, delete all the photos from the organizer. (Yeah, sounds radical I know). Take heart! All you've gotta do then is to reimport the whole folder. All the same name photos will be in one import batch. And with all the photos of any given topic in one folder, you'll be able to also use Windows Explorer in any number of modes to view the photos outside PSE.
    Keep in mind you can rename batchs of photos by selecting them, or by import batch or even tag, then "Ctrl. > Shift > N" will bring up the name change dialog box.
    This technique is quite contensious, but, it makes a whole lot more sense than storing all your photos in the "My Photos" folder on the "C:/" drive where any viral or malware hiccup might cause the necessity of a reformat, which could cause the loss of years of work.
    If a name search is a priority, that would be "Ctrl ."Shift" > "K" then type in whatever your heart desires.
    Caution: Before attempting a delete/ reimport manuver, Alway, always, write the tag and file info to the photo. "File" >"Write Tag and Properties Info to File".
    I always am quite adamantine about suggesting that you do NOT use the same physical HDD that contains Windows to store your photos, that's just asking for trouble. HDDs are quite cheap nowadays, and they're worth every penny in piece of mind. Plus to use a storage ("Volume") drive, you have to create your own file heirarchy anyway, so that leads right back to the beginning of this discussion.

  • I have an early MiniMac with OS X 10.4.11.  Can I get a free OS upgrade and which version should I choose?  Do I just download and over ride the current OS system or what?

    I have an early MiniMac with OS X 10.4.11.  Can I get a free OS upgrade and which version should I choose?  Do I just download and over ride the current OS system or what?

    First what model Mini do you have? That will determine how high y can go.
    How to identify Mac mini models
    In any event you will have to pay

  • Comparing 2 HashMap with the equals method

    Hi,
    I�m trying to compare 2 HashMap with the equls method like this:
    saveAnyWayMap.equals(keysMap)
    The 2 HashMaps contains the following values and keys:
    saveAnyWayMap: {P_PERSON.PA_TIDPKT=null, P_PERSON.P_PXXX92=xxx, P_PERSON.P_P
    XXX91=xx, P_PERSON.P_PXXX90=xxx}
    keysMap: {P_PERSON.PA_TIDPKT=null, P_PERSON.P_PXXX92=xxx, P_PERSON.P_PXXX91=
    xx, P_PERSON.P_PXXX90=xxx}
    Why dose the equals method return false?
    When P_PERSON.PA_TIDPKT is not null the equals method returns true. Can�t the values be null if you want to compare HashMaps or what is wrong?
    Can someone please help?
    Thanks!
    /Lisa

    Works fine when I use null objects in a hashmap. Are you sure it is the null values that gives you the problem? And is the key in both maps?
    What if the only key-value pair you have is P_PERSON.PA_TIDPKT and null?
    Do you use string objects as keys?

  • How do I over-ride the DETERMING GAPLESS PLAYBACK process

    I am so frustrated! Everytime I put music on my 30G iPod, iTunes begins this lengthy process of processing EVERY SONG in my library and determining it's gapless playback. Is there a way that I can over-ride this painstaking process? I have gone to preferences and dealt with the crossfade options, but nothing will stop it. It makes my computer run terribly slow and when I opt to quit iTunes it tells me "your iPod is updating, if you quit now the updates will not be saved" and the process will start all over.
    Desperate.
    Mac
    Mac
    Mac    

    If you just let it finish, it won't do it again. Depending on the size of your library, it may take a few hours, or longer.

  • How can I over ride the combination code to access the volume limit?

    I want to increase my volume limit but there is a combination lock that shows up and I dont know what the code is! Is there some way I can over ride this and increase my volume limit? It's hard to hear any music.

    The only way to bypass the volume limit passcode is to restore the device to factory settings in iTunes.
    Restoring iPod to factory settings
    B-rock

  • 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.

  • Over ride the drop-down value?

    Hello,
    I am developing an VISITOR Adobe Interactive Form, i have Designer 8.1 and Reader 9.0 in my Windows 7 Laptop. When i checked the menu EDIT-->form properties --> Compatability, its Adobe XML form File (XDP)
    When i opened this VISITOR interactive form in a browser, right clicking and checked form PROPERTIES, then i saw the "PDF Version: 1.6 (Acrobat 7.x)"
    I have a bunch of drop-downs in my interactive form, done by dragging & dropiing the 'Drop-down list' object, at the end i binded it with some data source, working fine, say its COUNTRY list.
    As soon as use opened the form, he saw the 'USA' prepopulated in the drop-down field, fine.
    User dropped down the entries, he saw a couple, like USA, Canada, UK, Spain, fine.
    But, now user want to enter / override this USA entry with his own value, like FRANCE, but, my form is not allowing, its behaving like a 'readOnly' field!
    Pls. let me know how to over ride a entry of drop-down values, as per user wanting value?

    Hello Srinivas,
    You can overide the entry of drop-down list by:
    selecting the "Allow Custom Text Entry" box of the field tab of the object palette
    of that drop-down list.
    Thanks,
    Debadas.

  • Can I have iPod over-ride the auto-lock setting?

    I use my iPhone to play music over my car stereo via the aux input, and have the auto-lock set to 5 minutes. I want to leave my auto-lock setting on, but have it not lock when the iPod is playing. I think some other apps know how to over-ride this, but my iPhone is always locking me out when I'm driving and it's not safe to be unlocking it while driving.
    Of course I can turn OFF auto-lock in settings, but I'd rather not have to do that every time.

    No.
    If you do not want it to lock, then turn auto lock off.

  • How do i over ride the master password ?

    I am having trouble with my mac book pro. My mom took her mac book pro to her friends house he said he set the password and now she nor he remebers what it is and I need to perform tons of updatest to her lap top and allow it to play videos and much more. But due to the password problem I can not!!! CAN ANYONE HELP ME PLEASE?? It will be so helpful thanks!
    I do not know that program its running but I know its mac that came with it. It is OSx lepord I believe or what came with just the mac book pro when purshased at best buy.

    Have you read for possible solutions over in the "More Like This" thread over here?----------------------->
    CAPS is considered shouting.  Please avoid.  We can all hear ya!  CAPS is also hard to read.  Most here will avoid posts that have CAPS. 

  • After I update my calendar on my PC how do I get the new version to load to iphone and ipad and over ride the info on them?

    After I update my calendar on my PC how do I get the new version on my iphone and ipad and not the version currently there?

    Hi Kirbs13,
    Thanks for using Apple Support Communities.  Your calendar should automatically update if you're using iCloud.  If it is not, see this article for troubleshooting steps:
    iCloud: Troubleshooting iCloud Calendar
    http://support.apple.com/kb/ts3999
    Cheers,
    - Ari

  • How can I over-ride the low ink warning on a 4630

    I want to continue printing with my black ink cartridge even though the warning appears about print quality. When I press "OK" nothing happens, the warning remains displayed and the printer will not print.

    Hi,
    When the ink is too low on the placed cartridge you cannot print till replacing it.
    You may use Reserve Mode by completely removing the depleted ink artridge from the device, then you may print using the installed cartridge.
    Say thanks by clicking the Kudos thumb up in the post.
    If my post resolve your problem please mark it as an Accepted Solution

Maybe you are looking for

  • Most of my music has been lost, how can i get it back?

    almost all of my music has been lost randomly when i openned my iTunes and i can't find a way to get it back!

  • How do I import GoPro video into Final Cut Pro X?

    I'm about to purchase Final Cut Pro X (10.1.4). Will I be able to import my GoPro files? Do I have to convert them to another file type? Has anybody imported GoPro video into their projects? I believe that GoPro Studio can convert the video files int

  • Language experts: Port Java Code to AS3?

    Hello out there, I have been asked whether it would be feasable to translate Java code to AS3. This is a bit difficult since I am not really an expert in Java and only a beginner wrt AS3. Looking at some code samples it seems doable (at least in prin

  • Multi selection N rows and N Columns

    i am using ADF 10g jdeveloper 10.1.3.5 i want make multi selection(Check box) in 3 row and N columns depend on size of iterator, can give example or ..... thanks

  • Moving Objects On Button Click

    i was just woundering how to make an object e.g. (Rectangle Block) move on the click of a button     private void btn_MoveLeftMouseClicked(java.awt.event.MouseEvent evt) {         //move left     }Thanks Rich