The method filter (predict Super employee >) in Java – stream type is not applicable to parameters ((E) – > {})
•
Java
How to set the value in Java 8 in the filter? I want to set emailid to null, where firstname is Raj How can I do this in Java 8?
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); } }). } }
Solution
The filter method should return Boolean. I don't think there should be any side effects In your case, a 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 streaming:
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 process the entire list and all the items returned change:
employees.stream() //get stream .map(e -> { if(e.getFirstName().equals("Raj")) { e.setEmail(null); } return e; }) .collect(Collectors.toList());
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
二维码