Why we can use Predicate parameter without importing it?

Tran Anh Minh :

Usually, when we use Predicate, we need to import java.util.function.Predicate. But in my case, if we use Predicate as a parameter in a method (removeIf()), we don't need to import Predicate. Why is that?

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) 
    {   
        List<String> list = new ArrayList<>();
        list.add("ABCDEFG"); 
        list.add("ABC");
        list.removeIf(s -> s.length() > 4);
        System.out.println(list);   // ABC
    }
}

Thank you.

Naman :

if we use Predicate as a parameter in a method (removeIf()), we don't need to import Predicate. Why is that?

Since Predicate is a FunctionalInterface that can be represented as a lambda without the use of any imports. The type of the lambda is inferred by the compiler.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329052&siteId=1