0

In the below code, Is there any way i can debug hashcode and equals methods implementation code.If yes, Where i need to put the break point.

PS: I have already attached the java source code(src.zip)

public static void main(String[] args) 
{
    List<String>list=new ArrayList<String>();
    list.add("ff");
    list.add("gg");

    System.out.println(list.hashCode());

    List<String>list1=new ArrayList<String>();
    list1.add("ff");
    list1.add("gg");

    System.out.println(list1.hashCode());

    System.out.println(list.equals(list1));
    System.out.println(list.hashCode()==list1.hashCode());

}

Thanks, Vivek

Vivek S
  • 53
  • 2
  • 8

2 Answers2

0

Default equal's and hashCode method of arrayList resides under java.util.AbstractList if you want to debug your code, you could set breakpoint in that class around those two methods.

Internally it does call's equals on each and every String within the list. And same applies for hashCode i.e. it calls hashCode on each and every String within the list.

In short, your code should work and you should find same hashcode for both list's and they both should be equal as well.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • Ya i did the same. But i am getting "Unable to install break point in java.util.AbstractList due to missing line numbers attributes. – Vivek S Nov 19 '14 at 11:05
  • I checked window--preferences--java--compiler as well.. The add line numbers check box is checked in classfile generation tab – Vivek S Nov 19 '14 at 11:07
  • see this for issue above http://stackoverflow.com/questions/957822/eclipse-unable-to-install-breakpoint-due-to-missing-line-number-attributes – SMA Nov 19 '14 at 11:08
  • It didnot help me.. I am unable to debug all jdk methods – Vivek S Nov 19 '14 at 12:02
0

ArrayList extends AbstractList and having a breakpoint at AbstractList class' hashCode method should work.

You can get a fair idea of how the hashCode is calculated.

This is the implementation of hashCode() in AbstractList class.

public int hashCode() {
    int hashCode = 1;
    for (E e : this)
        hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
    return hashCode;
}
gokkk
  • 1
  • 1
  • Oh wow... Note to self: Never add lists to hash-based structures. – Ordous Nov 19 '14 at 11:12
  • When ever i put break point in jdk methods. here, equals() of AbstractList class I am getting following error Unable to install break point in java.util.AbstractList due to missing line numbers attribute – Vivek S Nov 19 '14 at 11:57
  • When ever i put break point in jdk methods. here, equals() of AbstractList class I am getting following error Unable to install break point in java.util.AbstractList due to missing line numbers attribute – Vivek S Nov 19 '14 at 12:02
  • You can try adding the source path for jdk. In IntelliJ, you can attach the same by pressing F4, and pointing the jdk src.zip file against the source path tab. – gokkk Nov 19 '14 at 13:35