Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fastest way to loop through a group of strings

807580Nov 2 2009 — edited Nov 2 2009
I am trying to optimize a small class that loops through an array of strings and compares to see if another string equals one of the values, for example:
String test = "this is test5";
String array[] = {"test1","test2","test3, test4,test5,test6,test7,test8"};

for(int i = 0;i < array.length; i++)
{
 if (test.endsWith(array))
{
return array[i];
}
}
The array that I am using holds closer to 15 values, but this loop can be hit thousands of times a day.  My  question is, are there any faster ways to loop through multiple strings than using arrays?  How do array lists or maps compare?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2009
Added on Nov 2 2009
8 comments
633 views