Results 1 to 8 of 8
- 05-24-2011, 05:41 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
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
- 05-24-2011, 06:02 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 05-24-2011, 06:04 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
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.
- 05-24-2011, 06:15 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 05-24-2011, 06:19 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
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!
- 05-24-2011, 06:26 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Hence the Set, which has a nice method that might be useful for you in this (if you clone one of your sets).
- 05-24-2011, 06:32 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
ok
about performance, do you think that set can outwin map?
- 05-24-2011, 06:41 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Issues with comparing two generic objects
By FOUAD-SPIDER in forum Advanced JavaReplies: 0Last Post: 05-06-2011, 04:39 AM -
comparing Graphs and Comparing Matrix
By jetnor in forum New To JavaReplies: 0Last Post: 03-27-2011, 01:40 AM -
Homework help involving comparing Color objects to Strings
By SergeantJoKer in forum New To JavaReplies: 12Last Post: 09-25-2010, 06:13 AM -
Comparing List of maps
By thorne_ in forum New To JavaReplies: 1Last Post: 06-10-2009, 02:30 AM -
please i need the code of comparing these two array lists.
By raj reddy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 04-18-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks