Java Streams:关于收集到Map <string,object =“”>的问题

时间:2018-11-12 12:10:59

标签: java java-8 java-stream

我遇到了一个问题:

我已经创建了此流,我需要将其映射到createSound = async() => { try { soundObject = new Expo.Audio.Sound(); await soundObject.loadAsync(require('soundpath')); await soundObject.setVolumeAsync(0.3) await soundObject.setIsLoopingAsync(true) await soundObject.playAsync() } catch (error) { console.log("sound couldn't load") } }

Map<String, Object>

我收到以下消息,但我不太清楚:

private Map<String, Object> collectArguments(JoinPoint point) {
    CodeSignature signature = (CodeSignature) point.getSignature();
    String[] argNames = signature.getParameterNames();
    Object[] args = point.getArgs();

    return IntStream.range(0, args.length)
        .collect(Collectors.toMap(param -> argNames[param], param -> args[param]));
}

2 个答案:

答案 0 :(得分:18)

<form action="" method="POST"> Select no.of questions:<input type="number" name="que" value="que"><br> <br> Select no. of series: <input type="number" name="ser" value="ser"> <br><br> Select number type(in digits) <input type="number" name="digits" value="digits"> <br><br> Select operations:<br /> <input type="checkbox" id="add" name="operation" value="addition" id="check1"><label>Addition</label><br/> <input type="checkbox" id="sub" name="operation" value="substraction" id="check2"><label>substraction</label><br/> <input type="checkbox" id="add" name="operation" value="multiplication" id="check3"><label>Multiplication</label><br/> <input type="checkbox" id="add" name="operation" value="division" id="check4"><label>Division</label><br/> <br><br> <input type="submit" name="submit" value="Generate"><br> <br> </form> 没有接受IntStream的{​​{1}}方法。它只有一个具有以下签名的3个自变量collect方法:

Collector

也许您应该使用collect

<R> R collect(Supplier<R> supplier,
              ObjIntConsumer<R> accumulator,
              BiConsumer<R, R> combiner)

或者,如果您希望使用Stream<Integer>的{​​{1}}方法,它将看起来像这样:

return IntStream.range(0, args.length)
                .boxed()
                .collect(Collectors.toMap(param -> argNames[param],
                                          param -> args[param]));

collect

答案 1 :(得分:2)

Eran's answer的另一种选择(使用Stream<Integer>的第一个变体确实很整洁)将是首先将数组的内容映射到对象:

public class Argument {
  private final String argName;
  private final Object arg;

  public Argument(String argName, Object arg) {
    this.argName = argName;
    this.arg = arg;
  }

  public String getArgName() {
    return argName;
  }

  public Object getArg() {
    return arg;
  }
}

用于将该对象收集到地图的代码变得非常清晰简洁:

Map<String, Object> map = IntStream.range(0, args.length)
    .mapToObj(i -> new Argument(argNames[i], args[i]))
    .collect(Collectors.toMap(Argument::getArgName, Argument::getArg));

也许甚至可以将创建Argument的逻辑提取到自己的方法中:

private List<Argument> toArguments(JoinPoint point) {
  String[] argNames = ((CodeSignature) point.getSignature()).getParameterNames();
  return IntStream.range(0, point.getArgs().length)
      .mapToObj(i -> new Argument(argNames[i], point.getArgs()[i]))
      .collect(Collectors.toList());
}

这样做,您的collectArguments()方法将很简单:

private Map<String, Object> collectArguments(JoinPoint point) {
  return toArguments(point).stream().collect(toMap(Argument::getArgName, Argument::getArg));
}