Java 8 java 8中基于索引的for循环转换
是否可以将下面给出的for循环转换为Java8代码Java 8 java 8中基于索引的for循环转换,java-8,Java 8,是否可以将下面给出的for循环转换为Java8代码 Object[] args = pjp.getArgs(); MethodSignature methodSignature = (MethodSignature) pjp.getStaticPart() .getSignature(); Method method = methodSignature.getMethod(); Annotation[][] parameterAnnotation
Object[] args = pjp.getArgs();
MethodSignature methodSignature = (MethodSignature) pjp.getStaticPart()
.getSignature();
Method method = methodSignature.getMethod();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
StringBuilder methodArgs = new StringBuilder();
for (int argIndex = 0; argIndex < args.length; argIndex++) {
for (Annotation annotation : parameterAnnotations[argIndex]) {
if ((annotation instanceof RequestParam) || (annotation instanceof PathVariable) || (annotation instanceof RequestHeader)) {
methodArgs.append(args[argIndex] + "|");
} else if ((annotation instanceof RequestBody)) {
methodArgs.append(mapper.writeValueAsString(args[argIndex]) + "|");
}
}
}
您必须打开一个
InsStream
来迭代args
索引,然后创建每个arg
的SimpleEntry
,以及相应的注释(根据您的代码),然后您可以应用您的业务逻辑
IntStream.range(0, args.length)
.mapToObj(argIndex -> new AbstractMap.SimpleEntry<>(args[argIndex], parameterAnnotations[argIndex]))
.flatMap(objectSimpleEntry -> Arrays.stream(objectSimpleEntry.getValue()).map(annotation -> new AbstractMap.SimpleEntry<>(objectSimpleEntry.getKey(), annotation)))
.forEach(objectAnnotationSimpleEntry -> {
Annotation annotation = objectAnnotationSimpleEntry.getValue();
Object arg = objectAnnotationSimpleEntry.getKey();
if ((annotation instanceof RequestParam) || (annotation instanceof PathVariable) || (annotation instanceof RequestHeader)) {
methodArgs.append(arg + "|");
} else if ((annotation instanceof RequestBody)) {
methodArgs.append(arg + "|");
}
});
IntStream.range(0,参数长度)
.mapToObj(argIndex->new AbstractMap.SimpleEntry(args[argIndex],parameternotations[argIndex]))
.flatMap(objectSimpleEntry->Array.stream(objectSimpleEntry.getValue()).map(注释->新建AbstractMap.SimpleEntry(objectSimpleEntry.getKey(),注释)))
.forEach(objectAnnotationSimpleEntry->{
Annotation=objectAnnotationSimpleEntry.getValue();
Object arg=objectAnnotationSimpleEntry.getKey();
if((RequestParam的注释实例)| |(PathVariable的注释实例)| |(RequestHeader的注释实例)){
方法args.append(arg+“|”);
}else if((RequestBody的注释实例)){
方法args.append(arg+“|”);
}
});
是的,这是可能的。让我们看看你迄今为止都尝试了什么?拉文德拉·兰瓦拉完成了
IntStream.range(0, args.length)
.mapToObj(argIndex -> new AbstractMap.SimpleEntry<>(args[argIndex], parameterAnnotations[argIndex]))
.flatMap(objectSimpleEntry -> Arrays.stream(objectSimpleEntry.getValue()).map(annotation -> new AbstractMap.SimpleEntry<>(objectSimpleEntry.getKey(), annotation)))
.forEach(objectAnnotationSimpleEntry -> {
Annotation annotation = objectAnnotationSimpleEntry.getValue();
Object arg = objectAnnotationSimpleEntry.getKey();
if ((annotation instanceof RequestParam) || (annotation instanceof PathVariable) || (annotation instanceof RequestHeader)) {
methodArgs.append(arg + "|");
} else if ((annotation instanceof RequestBody)) {
methodArgs.append(arg + "|");
}
});