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
  • 34,993
  • 17
  • 75
  • 115
LPD
  • 2,833
  • 2
  • 29
  • 48

5 Answers5

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.

6502
  • 112,025
  • 15
  • 165
  • 265
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.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
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
  • 189,189
  • 101
  • 473
  • 713
rohan kamat
  • 583
  • 5
  • 12
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.

Community
  • 1
  • 1
mel3kings
  • 8,857
  • 3
  • 60
  • 68
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);
                }
            }
Aditya
  • 1,033
  • 3
  • 11
  • 24