• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the fastest way to loop in java in huge data structure  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He all
i have data structure that is list that in every element there is hashtable that contains key value pairs, there is no limit on the list size
and it can be 500 elements long , now the thing is i need to out put this structure content as string , to do simple loop on the list and drill
to each hashtable and get the key value as string will take very long time .
how can i optimize this task? and make this action with minimum cpu ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your requirement is to output the whole data structure (is it?), then surely you cannot avoid traversing the whole list and every hash map. You can use an Iterator or an enhanced "for" loop to do so.

If you're building up a huge String from the data, do use StringBuilder. This is better than StringBuffer because it avoids unnecessary synchronisation. Both are much better than String concatenation using "+" as that creates a lot of unnecessary temporary objects.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:

i have data structure that is list that in every element there is hashtable that contains key value pairs, there is no limit on the list size
and it can be 500 elements long , now the thing is i need to out put this structure content as string , to do simple loop on the list and drill
to each hashtable and get the key value as string will take very long time .
how can i optimize this task? and make this action with minimum cpu ?


What do you mean by "very long"? How much time did it take when you tried?

500 elements aren't particularly many - it shouldn't take longer than a few milliseconds to iterate through them, even when using a non-optimized approach.

Moving to our Performance forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!