gpt4 book ai didi

java - 如何检查任何数组列表是否包含特定字符串

转载 作者:行者123 更新时间:2023-12-01 19:34:18 25 4
gpt4 key购买 nike

我正在尝试使用以下代码:

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

我的数组列表包含所有类别名称:

例如:[“apple-tv-apple-tv-4k”,“apple-tv-apple-tv-4k”,“apple-tv-apple-tv”]

需要搜索此数组中包含的 apple-tv。我的代码给出错误,因为不包含特定类别的 apple-tv。

最佳答案

使用流:

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

如果您想生成一个仅包含名称中包含 apple-tv 的类别的新列表,请使用 filter:

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

关于java - 如何检查任何数组列表是否包含特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58407962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com