0

I want to speed up the my code. Quick status information:

  • There is more than one list (String) like _list1, _list2, _list3.

  • I try to find a word (String) in these lists.

  • If I find a word, I will use index of the word on the list.

Here is my code:

private static int foundIndex (String s) 
{
    if (_list1.contains(s)) {
        return _list1.indexOf(s);
    } else if (_list2.contains(s)) {
        return _list2.indexOf(s);
    } else if (_list3.contains(s)) {
        return _list3.indexOf(s);
    } else if (_list4.contains(s)) {
        return _list4.indexOf(s);
    }
...
...
...
...
    } else if (_list100.contains(s)) {
        return _list100.indexOf(s);
    }
    return -1;
}

How can I speed up the my code?

5
  • You can map s to an index in a Map<String, Integer>. Commented Aug 8, 2017 at 19:41
  • 1
    What Java version? Commented Aug 8, 2017 at 19:42
  • 1
    return index when list is not known ... isn't too much useable Commented Aug 8, 2017 at 19:46
  • 4
    I'm voting to close this question as off-topic because I think it belongs to Code Review Commented Aug 8, 2017 at 19:47
  • Have you heard of arrays? Having 100 fields named _list1, _list2, ..., _list99, and _list100 is really bad code. Commented Aug 8, 2017 at 19:50

3 Answers 3

2

A couple simple optimizations comes to mind:

1.replace the if (contains) then indexOf pattern with if (i = indexOf(s) >= 0) return i

2.add lookup data structure like a Map<String,Integer> and either use it instead of the lists or in addition to them by updating it whenever you add or change a list

Sign up to request clarification or add additional context in comments.

Comments

1

Add all your lists (String) in a List<String> and iterate on it :

private static int foundIndex (String s) {
   for (String currentList : lists){
       int indexOf = currentList.indexOf(s);
       if (indexOf != -1) {
          return indexOf;
       }
    }
    return -1;
}

2 Comments

return index when list is not known ... isn't too much useable (like in primary solution)
@Jacek Cz Possible. I don't know the need. With a few modification, it may return a custom class ListAndIndex that contains both the index and the List.
0

I changed the algorithm of my code to your suggestions. I was use list to much before, now I changed it. I use 2D String array. But code performance was 157% decrease.

New Code :

private static String[][] _lists = new String[200][100];

private static int foundIndex (String s) {
for (int i = 0; i < 200; i++) {
        for (int j = 0; j < 100; j++) {
            if (_lists[i][j].equals(s) == true) {
                return j;
            }
        }
    }
    return -1;
}

That's where the problem starts.

If the code I'm looking for is "_list[180][?]", it's hard to find it.

How can I speed up the my code?

Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.