1

私はこのコードを持っています:

Arrays.asList(1L, 2L, 3L, 10L, 20L, 30L, 100L)
      .stream()
      .map(Bytes::fromMegaBytes) // Function<Long, Bytes>
      .map(FileUtils::generateTempFileRunEx) // Function<Bytes, Path>
      .flatMap(path -> parameters.getAllocatedCredits() // Map<Object, Integer>
                                 .keySet()
                                 .stream()
                                 .map(group -> Pair.of(group, path))
              )
      .collect(Collectors.toList())
      .forEach(item -> { // item should be Pair<Object,Path>
        System.out.println(item.getKey() + ": " + item.getValue());
        // The method getKey() is undefined for the type Object
        // The method getValue() is undefined for the type Object
      });

メソッドの型パラメーターの場合、Javac または ECJ (その場合) が型を推測するのに失敗する可能性があることを私は知っています。

.flatMap(path -> parameters.getAllocatedCredits()
                           .keySet()
                           .stream()
                           .<Pair<Object,Path>> map(group -> Pair.of(group, path))

その特定のケースで、単純なケースのように見えるのに、ECJ が型を正しく推測しないのはなぜですか?

編集: javac(mavenを使用)でテストした後、回答を更新し、それが機能することを確認しました。

編集 (2):コードをリファクタリングしてこれを機能させる:

  .flatMap(path -> {final Stream<Pair<Object, Path>> w = parameters.getAllocatedCredits()
                             .keySet()
                             .stream()
                             .map(group -> Pair.of(group, path));
  return w;
  }

注: Pairは commons-lang から取得され、 を実装しMap.Entryます。

4

1 に答える 1