lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicholas Paldino (JIRA)" <>
Subject [jira] Commented: (LUCENENET-284) java vs .Net GetHashCode and Equals for ArrayList
Date Wed, 18 Nov 2009 19:06:39 GMT


Nicholas Paldino commented on LUCENENET-284:

A few things here.

- Collections helper should really be a different nested class.  I would make it EquatableList<T>
(or Comparable list if you want to implement IComparable<T> as well) which derives from
List<T>.  Then, implement IEquatable, IEquatable<T>, override the == and != operator,
and then override the Equals and GetHashCode methods to use the code you currently have in

- The EqualsByVal should be your override for the Equals method on the EquatableList (or if
you implement IComparable, just defer to the implementation of that being equal or not equal
to 0).

- There is an error in the EqualsByVal method.  When performing the equality test for the
items in the two lists, you are comparing references.  You should call the Equals method on
the implementation of IEquatable<T> returned from a call to EqualityComparer<T>.Default,
and use that as a result to determine equality.

- While you can use the & operator on boolean operands in some of the shortcut tests,
best pracices dictate that you use && as an operator instead, as most people expect
the short-circuit nature of the && operator.

I'm heading out, but if you don't do this, I'll do it later tonight and then integrate it
into the places where you made the calls to the new support class methods.

> java vs .Net GetHashCode and Equals for ArrayList 
> --------------------------------------------------
>                 Key: LUCENENET-284
>                 URL:
>             Project: Lucene.Net
>          Issue Type: Bug
>            Reporter: Andrei Iliev
>         Attachments: ArrayList.patch
> 1)In java the hash code of a list (and ArrayList) is defined to be the result of the
following calc:
> <code>
> hashCode = 1;
>   Iterator i = list.iterator();
>   while (i.hasNext()) {
>       Object obj =;
>       hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
>   }
> </code>
> In .Net it hash code of object itself.
> 2) In java two lists are defined to be equal if they contain the same elements in the
same order. 
> In .Net it compares the object references.

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message