Searching String Properties in Java ArrayList with Custom Objects

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: Java | ArrayList | Custom Objects | String Search | Stream API

Abstract: This article provides a comprehensive guide on searching string properties within Java ArrayList containing custom objects. It compares traditional loop-based approaches with Java 8 Stream API implementations, analyzing performance characteristics and suitable scenarios. Complete code examples demonstrate null-safe handling and collection filtering operations for efficient custom object collection searches.

Introduction

Working with collections of custom objects is a common requirement in Java programming. When searching for specific string properties within an ArrayList, developers need approaches different from basic type searches. This article provides an in-depth analysis of effective string searching techniques in custom object collections based on practical development scenarios.

Custom Object Structure Analysis

Consider a typical custom class Datapoint containing multiple string properties:

public class Datapoint implements Serializable {
  private String stateBased;
  private String name;
  private String priority;
  private String mainNumber;
  private String groupadress;
  private String dptID;

  public Datapoint() {}

  public String getMainNumber() {
    return mainNumber;
  }

  public void setMainNumber(String mainNumber) {
    this.mainNumber = mainNumber;
  }

  public String getName() {
    return name;
  }
  // Other getter and setter methods
}

The class implements the Serializable interface, indicating its instances can be serialized. Each property has corresponding getter and setter methods, following the standard Java Bean design pattern.

Traditional Loop-Based Search Method

The most fundamental search approach uses enhanced for-loop iteration:

List<Datapoint> dataPointList = new ArrayList<>();
String search = "target string";

for(Datapoint d : dataPointList) {
    if(d.getName() != null && d.getName().contains(search)) {
        // Perform relevant operations
        System.out.println("Found match: " + d.getName());
    }
}

Key aspects of this method include:

Java 8 Stream API Approach

For developers using Java 8 and later versions, Stream API offers a more functional solution:

List<Datapoint> myList = new ArrayList<>();
// Populate myList with data

List<Datapoint> filteredDataPoints = myList
    .stream()
    .filter(p -> p.getName() != null && p.getName().contains("target string"))
    .collect(Collectors.toList());

Advantages of the Stream approach include:

Performance and Scenario Analysis

The traditional loop method excels with small datasets, offering straightforward and intuitive code. For simple search requirements, this remains the most direct choice.

The Stream API method better suits complex data processing scenarios, particularly when multiple filter conditions or subsequent transformation operations are needed. While potentially having slight performance overhead with small datasets, it offers superior readability and maintainability.

Extended Search Functionality

Practical applications often involve more complex search requirements. Here are several extended scenarios:

// Searching multiple properties
for(Datapoint d : dataPointList) {
    if((d.getName() != null && d.getName().contains(search)) ||
       (d.getMainNumber() != null && d.getMainNumber().contains(search))) {
        // Process matching items
    }
}

// Using Stream for multi-condition search
List<Datapoint> multiFiltered = myList.stream()
    .filter(p -> (p.getName() != null && p.getName().contains(search)) ||
                 (p.getMainNumber() != null && p.getMainNumber().contains(search)))
    .collect(Collectors.toList());

Best Practice Recommendations

In actual development, consider:

Conclusion

Searching string properties in custom object collections is a fundamental yet crucial skill in Java. Traditional loop methods provide straightforward solutions suitable for most scenarios, while Stream API offers more modern functional approaches. Developers should choose appropriate methods based on specific requirements, data scale, and team preferences. Regardless of the chosen approach, null safety and code readability remain critical considerations.

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.