3

I have a string array in Java like this.

new String[] { "A", "AAD", "AC", "B" };

I want to search for a pattern like starts with 'A' and get back result in a array for all the items in the array that matched. I know this can be done by iterating the array and doing pattern search on each element.

But is there any efficient way to achieve the same?

Thanks.

Ruchira Gayan Ranaweera
35.7k17 gold badges78 silver badges117 bronze badges
asked Jun 14, 2013 at 5:34
2
  • 2
    Without traversing the array? I really doubt it. Commented Jun 14, 2013 at 5:36
  • 1
    Is there a measurable, visible, meaningful performance issue in your program, or are you speaking of hypotheticals? Commented Jun 14, 2013 at 5:49

5 Answers 5

1

You can write sql or x-path expression instead of iterating over collection - can choose library like JoSQL or q-Link or jXpath.

JoSQL

String[] strs = new String[] { "A", "AAD", "AC", "B" };;
List<String> stringList = Arrays.asList(strs);
String query = "SELECT * FROM java.util.String str where value Like 'A%'";

library actually iterate in optimized way over your collection.

answered Jun 14, 2013 at 5:51

1 Comment

Cool.. I didn know this :)
1

you could add some for of indexing to save time by not doing checks that will not match for sure. For example you can create 26 lists of indices for "A", "B", "C" and so on where the list for character x contains all indices of strings that contain at least one x.

When asked to search for a pattern you can check all the letters and pick the index that has the smallest number of indicies and only scan that.

This scheme can be made more sophisticated, for example storing an index list for each pair or triplet of characters. Depending of the number of strings that you need to search the speedup can be huge.

Of course the main assumption is that the list of strings is fixed, has many many elements and that you need to make many searches.

answered Jun 14, 2013 at 5:46

Comments

1

this use josql to query from collection

use this query "SELECT * FROM java.lang.String where toString $LIKE 'A%'"

example:

List<String> names=new ArrayList<String>();
String[] n={"Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };
Collections.addAll(names, n);
Query q=new Query();
 q.parse("SELECT * FROM java.lang.String where toString $LIKE 'A%'");
List<String> results=(List<String>)q.execute(names).getResults();
for(String name:results) {
 System.out.println(name);
}
Cheeso
193k106 gold badges486 silver badges734 bronze badges
answered Jun 14, 2013 at 7:51

Comments

0

Why not try Predicates? (From Google Guava)

There is already an explanation on how to use predicates here: Predicate in Java

But basically it creates a sorts of abstract iteration on a List, by creating a Predicate with the desired conditions, that acts as a filter, you don't actually iterate through it the Predicate does the heavy lifting.

answered Jun 14, 2013 at 6:00

Comments

0

Try this.

String [] strarray = new String[] { "A", "AAD", "AC", "B" };
 ArrayList<String> resultsList = new ArrayList<String>();
 Pattern pattern = Pattern.compile("The pattern U want to search for");
 for (String string : strarray) {
 Matcher matcher = pattern.matcher(string);
 if (matcher.find()) {
 resultsList.add(string);
 }
 }
answered Jun 14, 2013 at 6:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.