• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

collection query

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a collection which contains large number of objects and takes longer time to iterate.
I am new to java and just wondering how I can speed up the process.
Can I split the collection into 2 and iterate them in 2 treads? How can I do that?
Can I have two iterators for same collection to iterate from different locations?
Can someone help with an example on how to speed up my program?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the reason you have such a big collection, and why do you need to iterate over it?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a List you can use the subList method to get a portion of the List, which you can then iterate over. If you have a SortedSet you can use the headSet, subSet and tailSet methods to do something similar.

Note that you need to take care of synchronization when using the same collection from multiple threads. Even if you use one of the above methods, they are still connected to the original List / SortedSet. Just iterating over them shouldn't be a problem, but you'll run into problems (ranging from ConcurrentModificationExceptions to corruption of the collection internals) if you attempt to modify it without taking the proper precautions.
 
ronald dsouza
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob: Thanks for the reply. If possible can you show how can I make a sublist and iterate over it by using threads? I have never done that so an example will be helpful.

@Stephan: I am issuing a db query and it returns me a very large collection which I need to iterate and do processing on each item in collection.

 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how it's done:
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ronald dsouza wrote:@Stephan: I am issuing a db query and it returns me a very large collection which I need to iterate and do processing on each item in collection.


Are you aware that splitting the process over multiple threads can actually make it perform worse? If you have more threads than available processor cores (use the result of Runtime.getRuntime().availableProcessors()) the threads will have a lot more overhead because of context switching - pausing one thread, then active another. Just because you make your process use multiple threads doesn't mean that everything is done concurrently. In the end a processor core can only do one thing at a time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic