简体   繁体   中英

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM