HashCode() method implementation in KVM

hi, I've got a problem with the KVM implementation of Object.
More specifically I verified that Class Objects change their hash codes during a run session of the same VM which seems to be a violation of the rule
Whenever it is invoked on the same object more than once during
an execution of a Java application, the hashCode method
must consistently return the same integer, provided no information
used in equals comparisons on the object is modified.
written in the Object class doc
Suppose I define a class named MyClass. At different times during the execution of my application I may end up writing the following code.
Object obj1 = new MyClass().getClass();
System.out.println(obj1.hashCode());
// Sometime elapses
Object obj2 = new MyClass().getClass();
System.out.println(obj2.hashCode());
System.out.println(obj1 == obj2);What I see in the print out is that the two hashcodes are sometime different while the two objects references exactly point to the same Object consistently returning true as it should be.
Any hint for this misbehaviour?

Hey Hi,
    This method determines which entries are offered for selection in the
    dropdown list box for the salary statement. Entries can include, for
    example:
    o   All Salary Statements
    o   3 Salary Statements
    o   6 Salary Statements
    For each entry available for selection, you must specify the sequence
    number of the oldest salary statement displayed in the hit list for
    selection.
    As before, you define the standard selection for this selection list.
The following export parameters are available:
Parameter      -      Description
EX_MESSAGE -          Message
EX_VALUE_SET_EXT-     ESS: Enhanced ValueSet Structure
EX_DEFAULT_ENTRY -    Default entry in dropdown list
INCLUDE HRXSS_SER_VALUE_SET (ESS: Extended Value-Set Structure )
is nothing but the structurr which has following componants or you can say fields
VSKEY
VSVALUE
SEQNR
During the RUN times this fields can hold the value and export it to the portal as the Bussiness logic.

Similar Messages

  • HashCode on implementation of Set

    javadoc on Set<E> hashcode:
    "int hashCode()
    "Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hashcode of a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of the Object.hashCode method."
    Let's say I have a class Foo which is an implementation of Set<Bar>. Bar's hashCode is a perfect hash. But the sum of such hash codes won't be a perfect hash on Foo instances, and I'd like to have Foo's hashCode return a perfect hash. (A perfect hash is possible given the domain of Bar instances). If Foo's hashCode returns a value which meets the assurance stated in the doc, but is not the sum of the element hash codes, do I cause a potential client problem by violating what the hash code is "defined to be"?

    In addition to the excelent advice jverd gave I'd
    like to ask if beeing a Set<Bar> is Foo's primary
    job. Do clients view it as anything else, than a
    Set<Bar>? (If it's usually referenced from a
    variabled delcard "Foo foo;" and not "Set<Bar> foo",
    then chances are, that beeing a Set is not it's
    primary job).Yes, being a Set<Bar> is its primary, and probably only, job. The name of the class is (the equivalent of) BarSet.
    >
    If indeed Foo "is" "not just" a Set<Bar>, then you
    might want to remove that interface from it and
    produce a delegate, that acts as the Set.
    (foo.getSet())
    This way you could implement hashCode() of Foo the
    perfect way you want it to and have
    foo.getSet().hashCode() follow the requirements laid
    out by Set.hashCode()).
    This way you could fullfill both requirements.Or, as kajbj implies, I could fulfill both by just supplying a perfectHashCode(). I like that idea.
    Message was edited by:
    com.stevebrecher to correct misspelling of a name

  • HashCode method execution

    Dear Members :
    I have custom hashCode method in conjunction with overriden equals method, in my program and have following doubts/observations :
    [1] In order to verify whether the hashCode method is executed, I put a println statement within method, but this doesn't output when the program is run.
    [2] The program is a simple equality testing of two dates and doesn't make use of any map. I want to know when and who gives (implicit ?) call to this hashCode method, when equals method is overriden, I mean the program flow.
    Since SUN suggests to override hashCode, whenever equals method is overriden, and my program is not making use of map, how and when the hashCode method gets called and if so why my test print is not executed ?
    Thanks in advance.
    Atanu

    uj_ wrote:
    jverd wrote:
    uj_ wrote:
    One way to graciously avoid the contract is to override hashCode and let it throw an exception (or assertion) stating "hashCode not implemented. This class is not intended for use in hash-based collections" or something.What would be the advantage of doing that over just taking the 5 minutes needed to implement it properly?Who are you to stipulate other people's design needs? Just because something takes just 5 minutes doesn't mean it's the right thing to do.
    You may want a class to have an equals relation based on content but you still may not want the class to be in a hash-based collection. The most compelling reason may be that it's not immutable.
    The equals/hashCode contract is mostly a Sun promise regarding "standard" classes. Everybody else design their classes the way they see fit.Calm down UJ. I have as much right to offer my opinion as you have to offer yours. I just asked a simple question.
    The requirement that a class not be used in a hash-based collection would be silly. It's up to the user of the class how to store instances. If the fields that go into determining hashCode are mutable, then that should be a documentation issue. There are good reasons to keep hashCode and equals in sync. The only reason not to do so would be the time it takes to write the code. Since that time is trivial for any well-designed class, it's an extremely weak reason.

  • HashCode method

    How to write overridden hashcode method? Is there any particular rule for it?

    Folks,
    It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. My two bobs worth is... I'm a simple kind of idiot... I struggle with all the double negatives in Object.hashCode API. So here's my rewrite.
    The equals and hashCode methods share a special relationship.
    The general contract is:
    1. If two objects are equals they must have the same hashCode.
    2. BUT, If two objects have the same hashCode they don't necessarily have to be equals, but this is called "a hashCode clash", which ain't good. A poor hashCode implementation (one which produces a high proportion of "clashes") dramatically slows-down hashMap lookups. In hash-code-land: More distincter is more betterer.
    3. Both equals and hashCode must be consistent over time. The hashCode must remain unchanged unless an attribute effecting equals has changed... at which time the hashCode proabably should change (but see rule 2).
    The rules of thumb for developers are:
    1. The equals and the hashCode methods should examine the same attributes of the object.
    2. So... If you override hashCode you almost certainly need to override equals to fulfill the above contract.
    2. And vice versa: If you override equals you probably need to override hashCode to fulfill the above contract.
    And what the API should say but doesn't is that hashCode only affects Hash* collections (those which begin with the word "Hash") such as HashTable, and HashMap. If you're not using Hash* collections (and are positive that your objects will never be stored in a Hash* collection) then you can get away with totally ignoring hashCode and all of it's innate complexities; with the Caveat Emptor that surety is inversely proportional to the magnitude of the universe of discourse... so you're better off just doing it, coz it's easier than evaluating the consequences of NOT doing it.
    Just my two bobs worth.
    Cheers. Keith.

  • The hashCode() method

    As per the Java API documentation, the hashCode method returns distinct integers for distinct objects. In my code, which used this method for generating unique ids for a large number of objects, I came across a duplication of ids generated (i.e. the int returned by hashCode()).
    Is it possible for two different objects to have same values returned when hashCode method is invoked?
    Can any one please tell me where I can find the implementation code/detailed logic for this Java method?
    JRahul

    With the Identifiable interface.
    As the implementation of getID() should be the same in all of your class where you want to be able to get an unique ID, you could also either defines a abstract class instead of a interface, or create a parent implementing getID() and from which your classes may inherit.
    But in such cases, you cannot extends others class into yours.
    public class UniqueIDTest {
         private final int NUM_INSTANCE = 10;
         private final int NUM_CALL = 5;
         public UniqueIDTest() {
              UniqueID[] uniqueID = new UniqueID[NUM_INSTANCE];
              for (int i = 0; i < NUM_INSTANCE; i++) {
                   uniqueID[i] = new UniqueID();
              for (int i = 0; i < NUM_INSTANCE; i++) {
                   for (int j = 0; j < NUM_CALL; j++) {
                        System.out.println(uniqueID.getID());
         public static void main(String[] args) {
              UniqueIDTest uniqueUDTest = new UniqueIDTest();
    public class UniqueID implements Identifiable {
         public UniqueID() {     }
         public int getID() {
              return IDGenerator.getID();
    public class IDGenerator {
         private static int id = 0;
         private IDGenerator() {} //To avoid instanciation
         public static synchronized int getID() {
              return id++;
    public interface Identifiable {
         public int getID();

  • Accessing Sender parameter attribute in event hander method implementation

    Hello knowledgeable friends.
    I would like a single event handler to manage two alv grid objects.  There is a parameter SENDER that is available in the method implementations to say which object triggered the event.  I would like to get at the attribute MT_OUTTAB but this syntax does not work:
    local_variable = sender->mt_outtab
    Any help would be greatly appreciated

    Ok, MT_OUTTAB is a protected Attribute.  I would settle for just the name of the Sender.  This code checks:
        call method sender->get_name RECEIVING name = l_name.
    but l_name is empty.  I was hoping for 'GRID1'; when I created the object I used:
        CREATE OBJECT alvgrid
          EXPORTING
            i_parent = container_top
            i_name = 'GRID1'.

  • Creating shared library of native method implementation using gcc

    Hi
    I am using Dev-C++ IDE for writing and compiling C Programs. It supports gcc.
    I have to build a shared library of a native method implementation using gcc compiler.
    In JNI tutorial build script is given for VC++ which is as follows
    cl -Ic:\java\include -Ic:\java\include\win32
    -LD HelloWorldImp.c -Fehello.dll
    Similarly I wanted build script for gcc compiler.
    Thanks
    Shailesh

    Here is an example of building a library from one module with CygWin's gcc:
    # Must set JAVA_HOME and LIBRARY_MODULE before
    gcc -mno-cygwin -D__int64="long long" -I $JAVA_HOME/include/win32 -I $JAVA_HOME/include -shared -Wl,--kill-at -o $LIBRARY_MODULE.dll $LIBRARY_MODULE.c

  • Why should we overide hashCode method when we overide equals ?

    Why should we overide hashCode method when we overide equals ?

    java.lang.Swapnil wrote:
    Why should we overide hashCode method when we overide equals ?For the purpose of hashing, you need to override both hashcode and equal method. It is because different objects(in most cases) have different hashcode.
    Edited by: Hunky322 on 28.?ub.2009 16:21

  • Comparing dynamic fields of objects using equals and hashCode methods

    To compare the different objects of the same class with their contents like jobTitleId, classificationId, deptId & classificationId was to be done and do some manipulations later using Set and Map. I was able to do that by simply overriding the equals and hashCode methods of Object class and was able to fetch the information (like in the following Map).
        Map<LocationData, List<LocationData>>
    The following is the class I used (its been shown to you so that it can be referred for my problem statement):
    LocationData class
        package com.astreait.bulkloader;
        public class LocationData {    
            String locId, deptId, jobTitleId, classificationId;
            @Override  
            public boolean equals(Object obj) {        
                LocationData ld = (LocationData)obj;       
                return this.deptId.equals(ld.deptId) && this.jobTitleId.equals(ld.jobTitleId) && this.classificationId.equals(ld.classificationId) &&
        this.locId.equals(ld.locId);   
            @Override  
            public int hashCode() {        
                return deptId.hashCode() + jobTitleId.hashCode() + classificationId.hashCode() +locId.hashCode();  
    Problem:
    I'm already known to which all fields of this object I need to make the comparison.
    i.e I'm bound to use the variables named classificationId, deptId, jobTitleId & locId etc.
    Need:
    I need to customize this logic such that the fields Names (classificationId, deptId, jobTitleId & locId etc) can be pulled dynamically along with their values. So, as far as my understanding I made use of 2 classes (TableClass and ColWithData) such that the List of ColWithData is there in TableClass object.
    I'm thinking what if I override the same two methods `equals() & hashCode();`
    such that the same can be achieved.
        TableClass class #1
        class TableClass{
            List<ColWithData> cwdList;
            @Override
            public boolean equals(Object obj) {
                boolean returnVal = false;
                        // I need to have the logic to be defined such that
                        // all of the dynamic fields can be compared
                return returnVal;
            @Override
            public int hashCode() {
                int returnVal = 0;
                        // I need to have the logic to be defined such that
                        // all of the dynamic fields can be found for their individual hashCodes
                return returnVal;
    ColWithData class #2
        class ColWithData{
            String col; // here the jobTitleId, classificationId, deptId, locId or any other more fields info can come.
            String data; // The corresponding data or value for each jobTitleId, classificationId, deptId, locId or any other more fields.
    Please let me know if I'm proceeding in the right direction or I should make some any other approach. If it is ok to use the current approach then what should be performed in the equals and hashCode methods?
    Finally I need to make the map as: (Its not the concern how I will make, but can be considered as my desired result from this logic)
        Map<TableClass, List<TableClass>> finalMap;

    Hello,
    What is the relation with the Oracle Forms tool ?
    Francois

  • Understanding method implementation for LinkedList class

    Hello
    I'm new to java and just trying to get my head around data structures, particularly ArrayList and LinkedList implementations. I've looked at the Sun API for both classes, but would like to see how the methods are implmented so I can venture into writing my own versions of these classes..
    Does anyone know if there is a resource showing the full method implementation so I can view how they work?
    thanks,

    a really strange licence: look but don't touch, and it's still under copyrightThis license seems to make sense: you can look at it and learn from it but you are not supposed to make an own, incomptabile version.

  • Enums with constant-specific method implementation

    Just faced the following problem. I have a persistent class with one of the fields of it is enum with constant-specific method implementation:
    @Persistent
    public class Message
       static public enum Type
           DEFAULT
               @Override
               public String getDescription() { return "Some description"; }
           public abstract String getDescription();
       private Type type;
    }When I try to store the record, I get the exception:
    java.lang.IllegalArgumentException: Class could not be loaded or is not persistent: messages.Message$Type$1The problem seem to be in that compiler creates a separate class, namely Message$Type$1.class for the DEFAULT instance and this class is not known by BDB..
    If I remove the constant-specific method from enum, everything's working fine (as the ..$1.class is not created by compiler).. Except the fact that I'd like to have constant-specific methods there..
    Any ideas on this? Maybe it's a bad idea to create constant-specific methods if it means that each constant would get own class file (and they would bloat the storage routines)?

    Hi Mikhail,
    I recreated the problem here and you're right, constant-specific methods aren't working. This isn't something we thought about, to be honest, or tested.
    Just based on an initial quick look I see that for the compiler generated class, the Class.isEnum method returns false, which is why we don't recognize this as an enum and eventually why we throw the exception you're seeing. But assuming that we can identify the class as an enum (that shouldn't be too difficult) I don't know what other problems we will run into in trying to support this.
    For now I think the best thing is to avoid using the constant-specific methods. I have opened a ticket (#18357) so that we'll remember to look into this in more detail and see whether it can be supported in the future.
    If we are able to support it, then I'm hoping that we won't to store extra metadata for constants that have methods. In other words, I'm hoping that we won't have to add any extra storage or processing overhead.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Overriding hashcode method

    Can u pls tell me friends why should definetly i override the hashcode method if i override equals method of the Object class

    Because the contracts of equals and hashCode are strongly bound to each other.
    For each objects x and y where x.equals(y) returns true x.hashCode() must be equal to y.hashCode().
    If you don't follow this convention you'll get hard-to-find bugs as soon as you use your object as a key in some kind of HashMap/HashTable/...

  • HashCode method return same value for different string?

    Hello. I am finding hashCode method of string return same hashcode for many different string? It seem when string is long we get same value many times? What is solution? I am useing JDK 1.0.2. Thank you.

    Hello. I am finding hashCode method of string return
    same hashcode for many different string? There are about 4 billion possible hashcode values.
    How many possible strings do you think there are?
    Obviously many strings will have the same hashcode.
    What is
    solution?The solution is to not depend on hashcodes being unique.

  • Class HashSet and hashCode method

    I'm using a HashSet class with elements of class A.
    I define the equals method but when i add a object which is already in the set, this object is duplicate.
    Do i have to define the hashCode method in the class A? If yes, i don't understand why this method is used when adding a object.
    Can you give me a explanation ?

    Yes, you do. The hashCode method is used by HashSet and HashMap to find an object. See the API doc for Object, which explains the relationship between equals and hashCode.

  • Override hashCode() method

    hi,
    I'm writing a very simple hashCode() function (3different way), which uses 2 string to calculate the hashCode, it looks somethings like this:
    1)...
    public int hashCode() {
    str1 = "abc"; //Can be replaced by any string
    str2 = "cde"; //Can be replaced by any string
    return (str1 + str2).hashCode();
    }2...
    public int hashCode() {
    str1 = "abc"; //Can be replaced by any string
    str2 = "cde"; //Can be replaced by any string
    return (str1 + "-----" + str2).hashCode();
    }3...
    public int hashCode() {
    str1 = "abc"; //Can be replaced by any string
    str2 = "cde"; //Can be replaced by any string
    return (str1.hashCode() + str2.hashCode());
    }For 1 (similar for 2) if we have str1 + str2 = str1' + str2', then it will returns the same hashcode, which is I dont want...
    But for 3, will that results in clustering? What are the bad things about 3?
    Thanks

    I don't have any problem at the moment, I am just asking for opinion on the good and bad thing of each of those implementation on hashCode().
    My equals() methods is pretty obvious, if str1 = str1' and str2 = str2' then return true.
    The str1 & str2 can change.
    Anyway, let me put in the whole class code:
         public class MyClassTest {
              private final String str1;
              private final String str2;
              public MyClassTest(final String s1, final String s2) {
                   str1 = s1;
                   str2 = s2;
              public String getStr1() {
                   return str1;
              public String getStr2() {
                   return str2;
              public boolean equals(Object anotherMyClassTest) {
                   if (anotherMyClassTest instanceof MyClassTest) {
                        MyClassTest id = (MyClassTest) anotherMyClassTest;
                        if (id.getStr1().equals(this.getStr1()) && (id.getStr2().equals(this.getStr2()))) {
                             return true;
                   return false;
              public int hashCode() {
                   //See the 3 implementation below
         }and then, there was three way of implementing the hashCode()
    1)...
    public int hashCode() {
    return (str1 + str2).hashCode();
    2...
    public int hashCode() {
    return (str1 + "-----" + str2).hashCode();
    3...
    public int hashCode() {
    return (str1.hashCode() + str2.hashCode());
    }This MyClassTest will then be used inside a HashMap as the key (and there can be many of these)
    And my question is: Which of the 3 implementation is the best, and could you explain to me the good/bad thing of each of those 3 implementation?
    Thanks
    Message was edited by:
    Gurluver
    Message was edited by:
    Gurluver

Maybe you are looking for

  • Urgent: Error in magrating assets by LSMW

    When I use LSMW - convert data, the system end up a ABAP dump. What does it mean? Runtime Errors         UC_OBJECTS_NOT_CONVERTIBLE Date and Time          2008.05.23 10:28:56 Short text      Data objects in Unicode programs cannot be converted. 发生了什么

  • Settings in Process maintenance :ABAP program

    Hello Xperts, I have added ABAP program to one of the process chains in RSPC. I want to know what are the settings I have to check...I want the call mode ASYNCHRONOUS.. Can any one explain what are CALLED FROM and PROGRAM TO CALL tabs.(what I have to

  • Iphoto '10 question: disappearing photos

    My iphoto '10 is not properly uploading/deleting pictues from my digital camera. I can't find the lost photos as files anywhere on my computer. Anyone know what to do?

  • "RAM preview needs 2 or more frames to render playback" AE CC

    This issue started out of nowhere for what I consider to be no reason. Working on average sized 720p compositions with modest animations. I have tried all suggested solutions on this forum yet nothing has worked. I am running fresh comps in AE CC wit

  • Enhacement for VA01

    Hi!     In VA01 i want to insert a new field  with F4 functionality if you press F4 created sales order and contract to be displayed. thanks and regards. siva