Java - Optimize finding a string in a list
Posted
by
Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2011-01-10T05:51:57Z
Indexed on
2011/01/10
5:53 UTC
Read the original article
Hit count: 173
java
|optimization
I have an ArrayList of objects where each object contains a string 'word' and a date. I need to check to see if the date has passed for a list of 500 words. The ArrayList could contain up to a million words and dates. The dates I store as integers, so the problem I have is attempting to find the word I am looking for in the ArrayList.
Is there a way to make this faster? In python I have a dict and mWords['foo'] is a simple lookup without looping through the whole 1 million items in the mWords array. Is there something like this in java?
for (int i = 0; i < mWords.size(); i++) {
if ( word == mWords.get(i).word ) {
mLastFindIndex = i;
return mWords.get(i);
}
}
© Stack Overflow or respective owner