I am trying to convert this:
static Set<String> methodSet(Class<?> type) { Set<String> result = new TreeSet<>(); for(Method m : type.getMethods()) result.add(m.getName()); return result; }
Which compiles just fine, to a more modern version of Java 8 threads:
static Set<String> methodSet2(Class<?> type) { return Arrays.stream(type.getMethods()) .collect(Collectors.toCollection(TreeSet::new)); }
What causes the error message:
error: incompatible types: inference variable T has incompatible bounds .collect(Collectors.toCollection(TreeSet::new)); ^ equality constraints: String,E lower bounds: Method where T,C,E are type-variables: T extends Object declared in method <T,C>toCollection(Supplier<C>) C extends Collection<T> declared in method <T,C>toCollection(Supplier<C>) E extends Object declared in class TreeSet 1 error
I can understand why the compiler will have problems with this - there is not enough type information to figure out the conclusion. I do not see how to fix it. Somebody knows?
source share