Soorya Thomas
Soorya Thomas

Reputation: 401

How do I check if any arraylist contains specific string

I am trying with below code :

public void verifyCategorySlugsInSearchcall(BaseDTO baseDTO, String jsonResponse) {
        List<String> categoryList = JsonPath.read(jsonResponse,
                "response.groups.DEFAULT_GROUP.documents[*].categorySlugs[*]");
        CustomReporter.reportInfoWithOutLineBreak("Category from search call Response:" + categoryList);

        Assert.assertTrue(categoryList.size() > 0, "No category slug name was displayed.");

        CustomReporter.reportInfoWithOutLineBreak("Category Slug from  search call Response:" + categoryList);
        Assert.assertTrue(categoryList.contains(baseDTO.getPsCallDetails().getCategory()),
                "Category Name was not matching. Actual:" + categoryList + ".Expected:"
                        + baseDTO.getPsCallDetails().getCategory());
    }

My arrayalist contains all category name :

eg: ["apple-tv-apple-tv-4k","apple-tv-apple-tv-4k","apple-tv-apple-tv"]

Need to search apple-tv contains in this array. My code is giving error as not contains apple-tv in particular category.

Upvotes: 0

Views: 70

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522752

Using streams:

boolean result = categoryList.stream()
    .anyMatch(c -> c.contains("apple-tv"));

If you instead want to generate a new list containing only categories having apple-tv somewhere in the name, then use filter:

List<String> output = categoryList.stream()
    .filter(c -> c.contains("apple-tv"))
    .collect(Collectors.toList());

Upvotes: 2

Related Questions