#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Smile Question about linked list


    hello to everybody I have a linked list that contains objects each object contains two String field
    I want to know how to check if the object that i want insert is already exists in the linked list

    thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    108
    override equals() method of your object and then check if the object is already in the list.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,755
    Rep Power
    1576
    Remember, if you're going to override equals, don't forget hashcode either Java theory and practice: Hashing it out

    Comments on this post

    • Yawmark agrees
    • mrider agrees
    Open for extension, closed for modification
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    113
    Rep Power
    11
    Originally Posted by sunvsunv
    override equals() method of your object and then check if the object is already in the list.
    Doesn't the existing "contains" method work?

    Code:
        linkedList.contains(Object o);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,755
    Rep Power
    1576

    Doesn't the existing "contains" method work?
    How does contains know if the objects are equal or not? (hint: check out the API
    Open for extension, closed for modification
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0
    Hi! I think you can write an extra method to compare 2 strings are identical or not! Or you can override method compareTo(String s), which from Comparable<E> interface!

IMN logo majestic logo threadwatch logo seochat tools logo