The behavior of "equals" method of Arrays.asList() implementation

Discussion in 'Java' started by Alex J, Jun 28, 2011.

  1. Alex J

    Alex J Guest

    Basically I wonder if it is safe to use equals for collections (and
    junit's assertEquals in particular which in fact results in the
    *.equals invocation).

    One of collection instances I am working with is returned from
    Arrays.asList(..) method invocation.
    Sun JDK sources defines no equals method for
    java.util.Arrays.ArrayList inner class so I guess it is not safe to
    compare collections in that way.
    Surprisingly, equals called from Arrays.ArrayList "magically" (I can't
    find another word) checks the elements in the collection!
    I tried to walk throug the sources by using "step into" in the
    debugger, but it mysteriously transfer control to the equals method of
    my class (and yes, I have JDK sources attached, e.g. I can walk
    through implementation of certain String method).

    So the first question is: whether it is safe to use equals for *all*
    the JDK's collections?
    The second question is whether JVM adds "hidden" implementation in
    certain places and Arrays helper List implementation is one of those,
    so I may expect Arrays.asList(...) implementation behave in the same
    way on different JDKs?

    Thanks in advance!
     
    Alex J, Jun 28, 2011
    #1
    1. Advertising

  2. Alex J

    Stefan Ram Guest

    Alex J <> writes:
    >So the first question is: whether it is safe to use equals for *all*
    >the JDK's collections?


    What is »safe« supposed to mean in this case?

    The »equals« methods of the collections behave
    as documented in their Java SE docs, just like
    every other collection method does.

    >The second question is whether JVM adds "hidden" implementation in
    >certain places


    The compiler sometimes adds constructors so that
    the implementation behaves as specified in the
    JLS, but never »equals« methods.
     
    Stefan Ram, Jun 28, 2011
    #2
    1. Advertising

  3. Alex J

    Lew Guest

    Eric Sosman wrote:
    > Alex J wrote:
    >> Basically I wonder if it is safe to use equals for collections (and
    >> junit's assertEquals in particular which in fact results in the
    >> *.equals invocation).
    >>
    >> One of collection instances I am working with is returned from
    >> Arrays.asList(..) method invocation.
    >> Sun JDK sources defines no equals method for
    >> java.util.Arrays.ArrayList inner class so I guess it is not safe to
    >> compare collections in that way.

    >
    > Look again. The nested class extends AbstractList, and
    > inherits the equals() implementation from it.


    Not only that, but
    <http://download.oracle.com/javase/6/docs/api/java/util/List.html#equals(java.lang.Object)>
    so of course it worked, and it had better be safe or the implementation is
    fubared.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, Jun 28, 2011
    #3
  4. Alex J

    Roedy Green Guest

    On Tue, 28 Jun 2011 02:19:02 -0700 (PDT), Alex J <>
    wrote, quoted or indirectly quoted someone who said :

    >Basically I wonder if it is safe to use equals for collections (and
    >junit's assertEquals in particular which in fact results in the
    >*.equals invocation).


    Every object implements equals. Sometimes it amounts to == which many
    not be what you want.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One of the curses of the computer age is manufacturers now design
    home appliances to die on the very day the warranty expires.
    It is deliberate waste in the service of mindless profit.
     
    Roedy Green, Jun 28, 2011
    #4
  5. Alex J

    Arne Vajhøj Guest

    On 6/28/2011 6:18 AM, Stefan Ram wrote:
    > Alex J<> writes:
    >> So the first question is: whether it is safe to use equals for *all*
    >> the JDK's collections?

    >
    > What is »safe« supposed to mean in this case?
    >
    > The »equals« methods of the collections behave
    > as documented in their Java SE docs, just like
    > every other collection method does.


    The obvious answer.

    >> The second question is whether JVM adds "hidden" implementation in
    >> certain places

    >
    > The compiler sometimes adds constructors so that
    > the implementation behaves as specified in the
    > JLS, but never »equals« methods.


    The compiler is not the JVM, but your interpretation may
    be correct that his question included compiler.

    Arne
     
    Arne Vajhøj, Jul 22, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,917
    Chris Smith
    Jun 17, 2004
  2. Adam Monsen
    Replies:
    3
    Views:
    683
    Adam Monsen
    May 20, 2007
  3. Ken
    Replies:
    35
    Views:
    2,289
  4. laredotornado
    Replies:
    19
    Views:
    1,789
    Arne Vajhøj
    Nov 9, 2009
  5. Roedy Green
    Replies:
    2
    Views:
    158
    Stanimir Stamenkov
    May 10, 2014
Loading...

Share This Page