迁移java 7到java 8 - forEach in forEach in forEach 并导致HashMap?

2024-03-22

我的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(...?????);

你可以这样做stream and flatMap like:

 return classes.stream()
                .flatMap(c -> Arrays.stream(c.getDeclaredMethods()))
                .flatMap(m -> Arrays.stream(m.getAnnotations())
                        .filter(stepAnnotation -> stepAnnotation.annotationType().isAnnotationPresent(StepDefAnnotation.class))
                        .map(ann -> new AbstractMap.SimpleEntry<>(ann.toString(), m)))
                .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

迁移java 7到java 8 - forEach in forEach in forEach 并导致HashMap? 的相关文章

随机推荐