【发布时间】:2020-01-27 15:28:39
【问题描述】:
我的 java7 代码:
final Map<String, Method> result = new HashMap<>();
final Set<Class<?>> classes = getClasses(co.glue());
for (final Class<?> c : classes) {
final Method[] methods = c.getDeclaredMethods();
for (final Method method : methods) {
for (final Annotation stepAnnotation : method.getAnnotations()) {
if (stepAnnotation.annotationType().isAnnotationPresent(StepDefAnnotation.class)) {
result.put(stepAnnotation.toString(), method);
}
}
}
}
return result;
我尝试使用stream + flatMap + map + filter
result = classes.stream()
.flatMap(c-> c.getDeclaredMethods()
.stream())
.map(Annotation::getAnnotations)
.filter(...?????);
【问题讨论】:
-
@HadiJ 请不要像这样使用
forEach。要么正确使用流,要么坚持正常的嵌套 for 循环。 -
@marstran,同意!
标签: java arrays java-8 java-stream