Why is this wrong? About Java 8 streaming media
•
Java
public interface Filter<M> {
public interface Filter<M> { boolean match(M m); public static <T> Collection<T> filter(Collection<T> collection,Filter<T> filter) { return collection.stream().filter(filter::match).collect(Collectors.toList()); } //////////////////////////////////////////////////////////////// public static void main(String[] args) { ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8)); System.out.println(intList); List<Integer> list = filter(intList,null); System.out.println(list); } }
I'm learning the Java 8 streaming function. This is the code I have a problem with
I don't know why the parameter intlist doesn't match the filter () method Java should know < T > this is an integer, right?
Solution
I'm not sure why you encountered this particular error, but the problem is that your method declares that it will return collection < T >, but you try to assign the result to list < T > If you change the declaration of the filter to:
public static <T> List<T> filter(Collection<T> collection,Filter<T> filter)
... then it compiles without problem
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码