The method filter(Predicate<? super Employee>) in the type Stream<Employee> is not applicable for the arguments ((<no type> e) -> {})

java stream filter multiple conditions
java stream filter multiple fields
java predicate example
filter() method in java 8
java predicate filter
java 8 predicate example mkyong
filter predicate angular
not applicable for the arguments no type

How to set the values in Java 8 in filter ? I want to set emailId to null where firstName is Raj. How can I do that in Java8?

public class EmployeeDemo {
    public static void main(String[] args) {
        Employee e1 = new Employee("John", "Kerr", "john.kerr@gmail.com");
        Employee e2 = new Employee("Parag", "Rane", "john.kerr@gmail.com");
        Employee e3 = new Employee("Raj", "Kumar", "john.kerr@gmail.com");
        Employee e4 = new Employee("Nancy", "Parate", "john.kerr@gmail.com");

        List<Employee> employees = new ArrayList<>();
        employees.add(e1);
        employees.add(e2);
        employees.add(e3);
        employees.add(e4);

        employees.stream().filter(e -> {
            if(e.getFirstName().equals("Raj")) {
                e.setEmail(null);
            }
        }).
    }
}

Filter method should return boolean and I think shouldn't have any side effects. In your case simple loop will do the job:

for(Employee employee: employees) {
    if(e.getFirstName().equals("Raj")) {
        e.setEmail(null);
    }
}

But if you really want to use stream:

employees.stream() //get stream
    .filter(e -> e.getFirstName().equals("Raj")) //filter entries with first name Raj
    .forEach(e -> e.setEmail(null)); //for each of them set email to null

or (if you want to do process the whole list and return changed with all entries:

employees.stream() //get stream
    .map(e -> {
        if(e.getFirstName().equals("Raj")) {
            e.setEmail(null);
        }
        return e;
    })
    .collect(Collectors.toList());

Java Predicate Example - Predicate Filter, As I said, Predicate is functional interface. It mean we can pass lambda expressions wherever predicate is expected. For example one such method is filter() method from Stream interface. The filter() method receives the predicate as a parameter. Composing predicates. With and() and or() methods we can compose predicates in Java.

We can also use Collectors.mapping to check if the firstName is Raj and then set the email to null in the mapping function (1st argument) and then collect it in a new List(2nd argument):

employees.stream().collect(Collectors.mapping(emp -> {
          if("Raj".equals(emp.getFirstName())) {
              emp.setEmail(null); 
          } 
          return emp;
          }, Collectors.toList()));

And as nullpointer suggested, do not mutate the objects in the stream. You can use a copy constructor to make a copy of the Employee having firstName as Raj and setting the email property to null:

employees.stream().collect(Collectors.mapping(emp -> {
          if("Raj".equals(emp.getFirstName())) {
              return new Employee(emp.getFirstName(), emp.getLastName(), null);
          }
          return emp;
          }, Collectors.toList()));

Java Predicate - explaining predicates in Java, stream().filter(btf).forEach(System.out::println);. The predicate object is passed to the filter() method to get all� Filter method should return boolean and I think shouldn't have any side effects. In your case simple loop will do the job: How to negate a method reference

You could on the other hand use List.replaceAll as :

employees.replaceAll(employee -> {
    if (employee.getFirstName().equals("Raj")) {
        employee.setEmail(null);
    }
    return employee;
});

ERROR - The method filter(Predicate<? super String , ERROR - The method filter(Predicate) in the type Stream is not applicable for the arguments (String) The method filter expects the argument as the Predicate and not the String. Specify the Predicate as the filter function argument. predicate. test ("test1") returns true because it matches with "test1" passed to isEqual method; predicate. test ("test2") returns false because it does not match with "test1" passed to isEqual method; Please note that above comparison is done on the basis of String’s equals method. Filter list using Predicate

Using filter and map of Java8, you can do it like:

employees.stream()
         .filter(e -> e.getFirstName().equalsIgnoreCase("Raj"))
         .map(e -> {
              e.setEmail(null);
              return e;
       }).findAny();

Java Stream Filter with Lambda Expression, The filter() method is an intermediate operation of the Stream interface that allows us to filter elements of a stream that match a given Predicate:� Learn to use Stream.filter(Predicate condition) method to traverse all the elements and filter out all elements which do not match a given condition through Predicate argument. 1. Stream filter() method. This is a intermediate operation. Returns a stream consisting of the elements of this stream that match the given predicate.

Java 8 Predicate Chain, First, let's see how to use a simple Predicate to filter a List of names: As we can see, the syntax is fairly intuitive, and the method names� The filter() method is an intermediate operation of the Stream interface that allows us to filter elements of a stream that match a given Predicate: Stream<T> filter

How to Filter Collections in Java 8 with Streams and Predicates, As I said filter() method is defined in Stream class, it takes a Predicate object and returns a stream consisting of the elements from the original� Type Parameters: T - the type of the input to the predicate Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Uses of Interface java.util.function.Predicate (Java Platform SE 8 ), Methods in java.util with parameters of type Predicate. Modifier and Type, Method and Description removeIf(Predicate<? super E> filter). Removes all of the� As we can see, the syntax is fairly intuitive, and the method names suggest the type of operation. Using and(), we've filtered our List by extracting only names that fulfill both conditions. 5.2. Predicate.or()

Comments
  • You're missing a return statement in your filter lambda expression. You want filter(e -> e.getFirstName().equals("Raj"))
  • Suggestion: Avoid mutating elements within a stream.
  • filter() expects a bool result, you are not providing it as in your case nothing is being returned.
  • I want all the without Raj emailIds as well , basically I want all the records.
  • There’s a closing bracket missing in .forEach(e -> e.setEmail(null);