3 回答

TA贡献1799条经验 获得超6个赞
在这种情况下, contains 方法检查整个对象是否匹配。如果它包含特定的字符序列,则不会,因此 13 个字符的对象永远不会匹配 4 个字母的序列
代替
System.out.println(codes.contains("love"));//Im trying to check if the generated codes that have been stored in hashset have form a word
在每个单独的字符串上尝试 contains 方法,而不是 HashSet 的 contains 方法
for (String code: codes) {
if (code.contains("love") {
System.out.println("found!")
}
}

TA贡献1966条经验 获得超4个赞
像这样使用它
for (int x = 0; x < 100; x++) {
String generated = example.getAlphaNumeric(16);
codes.add(generated);
System.out.println(generated.contains("love"));
}

TA贡献1802条经验 获得超10个赞
您正在检查代码集中的完整字符串“love”。您需要在代码集中的每个代码中检查它是否存在。您可以使用流来检查这一点。
boolean isPresent = codes.stream().anyMatch( code -> code.indexOf( "love" ) != -1 );
添加回答
举报