我无法理解,如何为stream
构建谓词。
上课:
public class User {
@Getter
@Setter
private String firstName;
@Getter
@Setter
private String secondName;
}
另外,我有谓词课程:
public final class Predicates {
private Predicates() {
}
/**
* Returns a predicate, Not
*/
public static <T> Predicate<T> not(Predicate<T> predicate) {
return predicate.negate();
}
/**
* Returns a predicate, isNull
*/
public static <T> Predicate<T> isNull(Object o) {
return (o == null)
? ObjectPredicate.IS_NULL.callback()
: ObjectPredicate.ALWAYS_FALSE.callback();
}
//Enum for predicate object
enum ObjectPredicate implements Predicate<Object> {
IS_NULL {
@Override
public boolean test(Object o) {
return o == null;
}
},
ALWAYS_FALSE {
@Override
public boolean test(Object o) {
return false;
}
};
<T> Predicate<T> callback() {
return (Predicate<T>) this;
}
}
}
然后我想用这样的东西:
List<User> users = getSomeUserList();
Booleant check = users.stream().anyMatch(Predicate.isNull(User::getSecondName));
帮助我理解,我做错了什么。
答案 0 :(得分:3)
您可以使用map
将Stream<User>
映射到Stream<String>
,然后使用您的谓词搜索匹配项:
Boolean check = users.stream()
.map(User::getSecondName)
.anyMatch(Predicates::isNull);
答案 1 :(得分:0)
尝试以下方法:
function updateMetaThemeColor(theme) {
var themeColor;
if(theme == 'theme-1') {
themeColor = '#f4dcdc'
} else if(theme == 'theme-2') {
themeColor = '#f0e8dd';
} else if(theme == 'theme-3') {
themeColor = '#d5e7f0';
} else if(theme == 'theme-4') {
themeColor = '#f6efb9';
} else if(theme == 'theme-5') {
themeColor = '#eaeae8';
} else if(theme == 'theme-6') {
themeColor = '#f8e3c7';
} else if(theme == 'theme-7') {
themeColor = '#cde8e8';
}
//remove the current meta
$('meta[name=theme-color]').remove();
//add the new one
$('head').append('<meta name="theme-color" content="'+themeColor+'">');
}