javagenericsjava-8java-streamraw-types

how to recognize raw type


I have this exercise:

List<Integer> iList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
Predicate<Integer> p = x -> x%2 == 0; 
List newList = iList.stream()
                    .filter(p)
                    .filter(x -> x>3)
                    .collect(Collectors.toList()); 

System.out.println(newList);

In my opinion, newList is raw type and not generic, because it was initialized as List newList and not as List<Integer>. How is it possible to have a normal result for this exercise and not a compilation error?

If I write:

List iList = Arrays.asList(1,2,3,4,5,6,7);
iList.stream()
     .filter(x -> x%2==0)
     .filter(x -> x>3)
     .collect(Collectors.toList());

It doesn't compile. Why in the first case does the code compile?


Solution

  • List newList is just return type of the stream chain.

    Actually you are working with List<Integer> iList which is NOT raw type. Therefore there is no compile error