1

Using streamsupport with a Java 7 javac compiler I encounter the following compile error:

[ERROR] method map in interface java8.util.stream.Stream<T> cannot be applied to given types; [ERROR] required: java8.util.function.Function<? super java.lang.Object,? extends R>

[ERROR] found: <anonymous java8.util.function.Function<java.lang.Integer,java.lang.String>> [ERROR] reason: no instance(s) of type variable(s) R exist so that argument type <anonymous java8.util.function.Function<java.lang.Integer,java.lang.String>> conforms to formal parameter type java8.util.function.Function<? super java.lang.Object,? extends R>

My code is

List<Object> listSum = RefStreams.iterate(0, new UnaryOperator<Integer>() {
            @Override
            public Integer apply(Integer n) {
                return n+1;
            }
        }).limit(10000).map(new Function<Integer,String>() {
            @Override
            public String apply(Integer n) {
                return String.format("%04d", n);
            }

        }).collect(Collectors.toList());

I want to know what to do and why this error occurred? Thanks

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
  • please paste your code and not show screenshots. plus this is not a java-7 feature, it's java-8 – Ousmane D. Jan 08 '18 at 12:10
  • In Java 7 [Function](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html) is unknown / exists since java 8. As it seems you want all that nice stream functionality, yet another argument to switch to java 8/9. – Joop Eggen Jan 08 '18 at 12:14
  • what is `java8.util.stream`? is this a copy paste of the source of java8 which can be used in java7? – Lino Jan 08 '18 at 12:21
  • I imported streamsupport. – user8323440 Jan 08 '18 at 12:24
  • 2
    net.sourceforge.streamsupport streamsupport 1.6.0 – user8323440 Jan 08 '18 at 12:24
  • 3
    @Lino *is this a copy paste of the source of java8 which can be used in java7?* That description is not far-off. It's the [streamsupport library](https://github.com/streamsupport/streamsupport) which is just that. – Sartorius Jan 08 '18 at 14:42

1 Answers1

6

You'll have to be prepared that type inference in Java 6 / 7 compilers is not up to par with Java 8 / Java 9 compilers. So, sometimes a statement / expression that compiles with 8 can't be compiled unchanged with 6 or 7. Providing a type witness usually helps the compiler figure out the correct types in Java 6 / 7.

I would do it this way (this works for me with javac from Oracle JDK 1.7.0_80):

List<String> list = RefStreams.<Integer, Integer>iterate(0, new UnaryOperator<Integer>() {
    @Override
    public Integer apply(Integer n) {
        return n + 1;
    }
}).limit(10000).map(new Function<Integer, String>() {
    @Override
    public String apply(Integer n) {
        return String.format("%04d", n);
    }
}).collect(Collectors.<String>toList());

Note the two type witnesses here:

RefStreams.<Integer, Integer>iterate and Collectors.<String>toList.

The first is there to help the compiler infer the correct type for the map call and the second is there so that the result is correctly inferred as List<String> instead of List<Object>.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38