A better solution is to copy the String[] to HashSet<String> , then use the contains method of the HashSet .
Made an artificial example. Perhaps the actual data results will differ.
private static final int STRINGS_COUNT = 100 * 1000, TESTS_COUNT = 100 * 1000; private static final HashSet<String> hashSet = new HashSet<>(); private static final String[] massStringA = new String[STRINGS_COUNT]; public static void main(String[] args) { for (int i = 0; i < STRINGS_COUNT; i++) { massStringA[i] = createString(i); } long startTime = System.currentTimeMillis(); hashSet.addAll(Arrays.asList(massStringA)); System.out.println("Copy time: " + (System.currentTimeMillis() - startTime) + "ms"); Random rand = new Random(); String[] testStrings = new String[TESTS_COUNT]; for (int i = 0; i < TESTS_COUNT; i++) { int randValue = rand.nextInt(STRINGS_COUNT * 10); testStrings[i] = createString(randValue); } startTime = System.currentTimeMillis(); int matches = 0; for (int i = 0; i < TESTS_COUNT; i++) { if (existA(testStrings[i])) { matches++; } } System.out.println("Array search time: " + (System.currentTimeMillis() - startTime) + "ms, matches: " + matches); startTime = System.currentTimeMillis(); matches = 0; for (int i = 0; i < TESTS_COUNT; i++) { if (Arrays.asList(massStringA).contains(testStrings[i])) { matches++; } } System.out.println("Arrays.asList search time: " + (System.currentTimeMillis() - startTime) + "ms, matches: " + matches); startTime = System.currentTimeMillis(); matches = 0; for (int i = 0; i < TESTS_COUNT; i++) { if (hashSet.contains(testStrings[i])) { matches++; } } System.out.println("HashSet search time: " + (System.currentTimeMillis() - startTime) + "ms, matches: " + matches); } private static String createString(int number) { int value = number; StringBuilder sb = new StringBuilder(); while (value > 0) { sb.append((char)(value % 80 + 47)); value /= 80; } return sb.toString(); } private static boolean existA(String a) { for (String s : massStringA) { if (a.equals(s)) { return true; } } return false; }
For 100 thousand tests with 100 thousand lines, the results are as follows:
Copy time: 16ms Array search time: 69762ms, matches: 9858 Arrays.asList search time: 73248ms, matches: 9858 HashSet search time: 14ms, matches: 9858
For a million tests with 10 thousand lines:
Copy time: 3ms Array search time: 17775ms, matches: 99845 Arrays.asList search time: 17537ms, matches: 99845 HashSet search time: 26ms, matches: 99845