您可以编写自己的匹配器:
private static List<String> FIELDS=Arrays.asList("one","two","three","four");
String myString="This string may contain 'one' word or 'two' word";
boolean stringDoesNotContainItem=filter(createIsInStringMatcher(myString),FIELDS).isEmpty();
和:
private TypeSafeMatcher<String> createIsInStringMatcher(final String string) {
return new TypeSafeMatcher<String>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(String item) {
return string.indexOf(item) >= 0;
}
};
}
编辑:还有另一种 Lambdaj 方法exists
允许以更紧凑的方式编写它:
List<String> FIELDS = Arrays.asList("one", "two", "three", "four");
String myString = "This string may contain 'one' word or 'two' word";
boolean stringContainsItem = Lambda.exists(FIELDS, createIsInStringMatcher(myString));