1

Problem Description


I have list of strings, which contains 8000 items. Items which contains list are described below.

List<String> stringList = new List<String>(8000);
stringList.add("this is first string.");
stringList.add("text which I want to search.");
stringList.add("separated string items.");
....

So you can see that every item in my list is a sentence which have more then three words.

Question.


User from outside can search through the list in the following way. For example user want to search word "first" the search algorithm must work in this way.

Search algorithm must run over the list and compare word "first" with all words in the sentence and if any word in the sentence starts with "first" it must return that sentence ". So in order to realize this algorithm I write following code, you can see code below.

Algorithm which I implement works very slow, So I want to know if there is faster algorithm or how I can made my algorithm faster ?

Code Example


Iterator<ContactInformation> stringListIter  = stringList .iterator();
while (stringListIter.hasNext()) {
            
    String currItem = stringListIter.next();
    String[] separatedStr = currItem.split(" ");

    for(int i=0; i<separatedStr.lenght; ++i)
        if(separatedStr[i].startsWith(textToFind))
            retList.add(currItem);  
}
Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147

4 Answers4

3

You could use the String#contains method along with String#startsWithinstead of splitting your String and searching each token.

String currItem = stringListIter.next();
if(currItem.startsWith(textToFind.concat(space))){
    retList.add(currItem);
} else if(currItem.endsWith(space.concat(textToFind))){
    retList.add(currItem);
} else if(currItem.contains(space.concat(textToFind).concat(space))){
    retList.add(currItem);
} else if(currItem.equals(textToFind)){
    retList.add(currItem);
}

First if - Checks if its the first word.

Second if - Checks if its the last word.

Third if - Checks if its somewhere in the middle.

Last if - Checks if its the only word.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Contains is not work in a same way like start with, `if(currItem.contains(textToFind))` this work fast, but this is not my case. – Viktor Apoyan May 02 '13 at 07:39
  • @ViToBrothers - Check the update! I hope it solves your problem. You can use a `regex` too, but I'm not an expert in `regex`, so you need to ask help elsewhere, in that case! – Rahul May 02 '13 at 08:38
2

I would hold a Map<String, Set<Integer>> where every word is a key and the value are the indexes of the sentence that contains this word.

Daniel Stucki
  • 203
  • 1
  • 7
1

A task perfectly suited for Lucene.

jontejj
  • 2,800
  • 1
  • 25
  • 27
1
for(String s : yourList){
    if(s.contains(textToFind)){
        retList.add(s);
    }
}
Mathiewz
  • 11
  • 1