This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to compare 2 Lists effectivley

Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have 2 collections, first holding the list of ticket’s Ids like T1, T2, T3 etc.

Second holds the list of objects which is TicketUsageDetails. This object has ticket-id in it as well as some other attributes.

Now I have a method getTicketDetailsByUsingTicketIDs, which accepts the first collection (list of ticketIds) as an input parameter.
Then this method calls the DAO method, which uses this list of ticketIds in the SQL queries ‘IN’ criteria.
This DAO method returns the list of TicketUsageDetails objects.

So now my input parameter list has list of ticketIds and the list which I got back from DAO has list of TicketUsageDetails, which has ticketIds in it.

Now I have to create a Map out of these2 lists, which will have structure like MAP<String ticketed, List< TicketUsageDetails>.

To create this map, at present I am looping both the lists but this is taking too much time, since for every TicketId of the input parameter list, I have to traverse inner list of TicketUsageDetails that many times.

At high level steps of my method as follows

I want to replace this looping logic with some good performance efficient way.
Please do let me know if you have any option.
Posts: 3036
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way would be to sort both lists. That would change the time from O(m*n) to O(m log m + n log n + m+n) where m and n are the lengths of the two lists.* (Do you see how?) Another way would to be store the second list in a HashMap structure, which would allow you to find an element in constant time. In theory, that reduces the time to O(n), but there's O(m) time to build the HashMap. That could be reusable though, depending on what you're doing.

You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic