Results 1 to 10 of 10

Thread: Collections

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Collections

    The question is about collections interface.

    From the Complete Reference Java book

    boolean equals(Object obj): Returns true if the invoking collection and obj are equal. Otherwise returns false
    How can a collection be compared to an object? If, I am not wrong collection consists of objects. So how can a collection of objects be compared to a single object. Is there a printing error in the book or am I missing something?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Collections

    You're missing that everything in Java is an Object, apart from primitives ;)

    boolean same = list1.equals(list2);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Collections

    What blew my mind way back when is that even a Class instance is an object. Try to explain that in a conversation.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Collections

    Ok, then is the following usage of equals method correct?

    Java Code:
    List l1= new ArrayList();
    l1.add("Ram");
    l1.add("a");
    l1.add("is");
    l1.add("boy");
    l1.add("also");
    System.out.println(l1.equals("a"));

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Collections

    Ok, then is the following usage of equals method correct?
    Yes, although it'll return false as l1 is a List and "a" is a String.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Collections

    Then the author should have mentioned :
    Invoking object and Object obj are compared
    and/or
    Invoking collection and Collection c are compared

    Do you agree?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    732
    Rep Power
    2

    Default Re: Collections

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Ok, then is the following usage of equals method correct?
    As, PhHein says the usage is correct, but if you are trying to find out if "a" is in the list, then you might be looking in the wrong direction. A List has it's own methods to see if the it contains some object (which by the way uses equals() as well). If you really are checking if the List is the same as a String, then by all means go ahead :)

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Collections

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Then the author should have mentioned :
    Invoking object and Object obj are compared
    and/or
    Invoking collection and Collection c are compared

    Do you agree?
    No. Equals is always from a specific object type to an Object.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Collections

    Rajeev Herbert is best programming author ... don't try to make him wrong .. try to understand his point ... it will be helpful ...

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Collections

    Quote Originally Posted by allaudin View Post
    Rajeev Herbert is best programming author ... don't try to make him wrong .. try to understand his point ... it will be helpful ...
    Don't oversell it dude, the best does not exist. But Herbert is certainly one of the better authors, without him I'd probably never have learned C++ many years ago :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 08:12 AM
  2. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 11:14 PM
  3. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 05:29 PM
  4. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM
  5. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •