String Search in Java ArrayList: Comparative Analysis of Regular Expressions and Multiple Implementation Methods

Dec 05, 2025 · Programming · 10 views · 7.8

Keywords: Java | ArrayList | String Search | Regular Expressions | Stream API

Abstract: This article provides an in-depth exploration of various technical approaches for searching strings in Java ArrayList, with a focus on regular expression matching. It analyzes traditional loops, Java 8 Stream API, and data structure optimizations through code examples and performance comparisons, helping developers select the most appropriate search strategy based on specific scenarios and understand advanced applications of regular expressions in string matching.

Introduction

In Java programming, ArrayList is one of the most commonly used collection types, often requiring search operations on its string elements. This article is based on a typical problem scenario: how to search for a specific substring in an ArrayList containing strings and return all matching items. The original problem requires searching for the substring "bea" and returning ["bear", "beat"] from the list ["behold", "bend", "bet", "bear", "beat", "become", "begin"]. Building on this, we systematically explore multiple implementation approaches.

Core Solution: Regular Expression Matching

According to the best answer (score 10.0), regular expressions provide the most precise and flexible matching method. Below is the complete implementation code:

List<String> list = new ArrayList<>();
list.add("behold");
list.add("bend");
list.add("bet");
list.add("bear");
list.add("beat");
list.add("become");
list.add("begin");

List<String> matches = new ArrayList<>();
for (String str : list) {
    if (str.matches("(?i)(bea).*")) {
        matches.add(str);
    }
}
System.out.println(matches); // Output: [bear, beat]

Code analysis:

The advantage of this method lies in the powerful matching capabilities of regular expressions, which can be easily extended to more complex pattern matching, such as using ^bea.* to match strings starting with "bea".

Comparative Analysis of Alternative Approaches

Besides regular expressions, several other common methods exist, each suitable for different scenarios.

Approach 1: Traditional Loop with contains Method

The answer scoring 6.2 proposes a simple implementation using the contains() method:

ArrayList<String> result = new ArrayList<>();
String searchString = "bea";
for (String current : list) {
    if (current.contains(searchString)) {
        result.add(current);
    }
}

This method is more intuitive and easier to understand but offers relatively limited functionality, supporting only simple substring matching without the complex pattern matching capabilities of regular expressions.

Approach 2: Java 8 Stream API

The answer scoring 5.3 demonstrates modern Java's functional programming style:

List<String> matches = list.stream()
    .filter(str -> str.contains("bea"))
    .collect(Collectors.toList());

Advantages:

Combined with regular expressions, further optimization is possible:

List<String> matches = list.stream()
    .filter(str -> str.matches("(?i).*bea.*"))
    .collect(Collectors.toList());

Approach 3: Data Structure Optimization

The answer scoring 2.8 mentions HashSet, which, while not directly solving substring search, provides insights into performance optimization:

Set<String> set = new HashSet<>(list);
if (set.contains("bend")) {
    System.out.println("String found!");
}

For exact matches (rather than substring matches), HashSet's contains() operation has O(1) time complexity, far superior to ArrayList's O(n). This suggests that when designing systems, appropriate data structures should be selected based on specific requirements.

Performance Analysis and Optimization Recommendations

Performance characteristics of different methods:

  1. Regular Expression Matching: Most powerful functionality but relatively higher performance overhead; suitable for complex pattern matching
  2. contains() Method: Better performance; suitable for simple substring matching
  3. Stream API: Concise code; parallel streams can improve performance for large datasets
  4. startsWith() Method: Optimal performance if only prefix matching is required

Optimization recommendations:

Practical Application Extensions

In actual development, string search requirements may be more complex:

// Case-insensitive search
Pattern pattern = Pattern.compile("bea", Pattern.CASE_INSENSITIVE);

// Multi-condition search
List<String> multiMatches = list.stream()
    .filter(str -> str.contains("bea") || str.contains("ben"))
    .collect(Collectors.toList());

// Custom matching logic
List<String> customMatches = list.stream()
    .filter(str -> str.length() > 3 && str.startsWith("be"))
    .collect(Collectors.toList());

Conclusion

There are multiple ways to implement string search in Java ArrayList. The choice depends on specific requirements:

  1. Regular expressions are the best choice for complex pattern matching
  2. Simple substring matching can use the contains() method
  3. Modern Java projects recommend Stream API for better readability and parallel processing capabilities
  4. Performance-critical scenarios should consider data preprocessing and index optimization

By understanding the principles and applicable scenarios of these methods, developers can make more informed technical choices and write code that is both efficient and maintainable.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.