Results 1 to 8 of 8
  1. #1
    LucasH is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default Comparing objects: lists and maps

    Hi guys! I'm having a big problem (both from a programmatic and efficiency point of view). Let me show you my problem.
    On my application, I mainly work with the following object:
    Name
    "Page"
    Attributes
    String url
    String extraParameters
    int depth

    On my application, there's a method that needs to check if Page objects from list "A" are on list "B". The way to know this is through their String url attribute. Example

    ArrayList A
    - Object1: attributeA, attributeB, attributeC
    - Object2: attributeD, attributeE, attributeF
    - Object3: attributeG, attributeH, attributeI


    ArrayList A
    - Object4: attributeA, attributeB, attributeC
    - Object5: attributeJ, attributeK, attributeL
    - Object6: attributeM, attributeN, attributeO

    The problems are facing are:
    - How can I search for an object on a ArrayList? The list is NOT sorted
    - I'm 100% sure that using a Map is better. Can somebody help me?


    Thanks, and feel free to ask for more specifications
    -Lucas

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Can you have two Page objects in a List that have the same url, but with different extraParameters or depth attributes, or is there only ever going to be one?
    What else do you do with these Lists?

    I mean, for this immediate problem (presuming that overriding equals() so that a Page is equal to another one if the urls are the same is not correct), then a Map<String, List<Page>> where the key is the url might be the way to go? Then just compare key sets.

  3. #3
    LucasH is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Great question!
    The idea is that each URL is going to be unique. So, maybe using a Map<String, Page> (where the String is the url attribute) is the best idea.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    If each URL is unique then stick with the List and simply override equals() such that if the urls are the same then return true.
    That will allow you to use contains() for List.
    Indeed maybe a Set would be better.
    Override hashcode to return url.hashcode() and use a HashSet.

  5. #5
    LucasH is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    but how come a List is better than a Map here? If I use a list, I would have to iterate all over the list to get it, but if I use a Map it'd be more direct!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Hence the Set, which has a nice method that might be useful for you in this (if you clone one of your sets).

  7. #7
    LucasH is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    ok
    about performance, do you think that set can outwin map?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You shouldn't be thinking about that.
    You should be thinking "does a Map or a Set make sense".

    Which comes down to what else you are using these collections for.
    If nothing else, then a Set since that's what you want to do...set operations (see retainAll()).

Similar Threads

  1. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 05:39 AM
  2. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 02:40 AM
  3. Homework help involving comparing Color objects to Strings
    By SergeantJoKer in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 07:13 AM
  4. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 03:30 AM
  5. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Tags for this Thread

Posting Permissions

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