• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection query

 
ronald dsouza
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Bartender
Pie
Posts: 5371
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the reason you have such a big collection, and why do you need to iterate over it?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how it's done:
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic