Java Streams中的lambda函数集合 [英] Collection of lambda functions in Java Streams
本文介绍了Java Streams中的lambda函数集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个流功能KStream<K, V>[] branch(final Predicate<? super K, ? super V>... predicates)
.我想动态创建谓词列表.有可能吗?
I have a stream function KStream<K, V>[] branch(final Predicate<? super K, ? super V>... predicates)
. I wanted to create a list of predicates dynamically. Is that possible?
KStream<Long, AccountMigrationEvent>[] branches = stream
.map((key, event) -> enrich(key, event))
.branch(getStrategies());
[...]
private List<org.apache.kafka.streams.kstream.Predicate<Long, AccountMigrationEvent>> getStrategies() {
ArrayList<Predicate<Long, AccountMigrationEvent>> predicates = new ArrayList<>();
for (MigrationStrategy strategy : strategies) {
predicates.add(new org.apache.kafka.streams.kstream.Predicate<Long, AccountMigrationEvent>() {
@Override
public boolean test(Long key, AccountMigrationEvent value) {
return strategy.match(value);
}
});
}
return predicates;
}
推荐答案
我尚未测试此代码,但从理论上讲,它应该可以工作:
I haven't tested this code but in theory it should work:
//All the predicates mentioned in here are of type org.apache.kafka.streams.kstream.Predicate
private Predicate<Long, AccountMigrationEvent>>[] getStrategies() {
List<Predicate<Long, AccountMigrationEvent>> predicates = strategies.stream()
.map(strategy -> (Predicate<Long, AccountMigrationEvent>>) (key, value) -> strategy.matches(value))
.collect(toList());
// branch() method on KStream requires an array so we need to transform our list
return predicates.toArray(new Predicate[predicates.size()]);
}
这篇关于Java Streams中的lambda函数集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文