I Have two Class as follows
package academy.learnprogramming;
public class Employee {
int empId;
int salary;
String name;
String designation;
Employee(int empId, String name, String designation, int salary){
this.empId = empId;
this.name = name;
this.designation = designation;
this.salary = salary;
}
public String toString(){
return "ID = "+empId + ", Name = "+name+", Designation = "+designation+", Salary = "+salary;
}
public String filterData(){
if(empId == 111 && salary > 1000){
return "ID = "+empId + ", Name = "+name+", Designation = "+designation+", Salary = "+salary;
}
return "";
}
}
package academy.learnprogramming;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static List<Employee> employeeList = new ArrayList<Employee>();
public static void main(String[] args) {
Employee employee1 = new Employee(111,"Niranjan","SSE",10000);
Employee employee2 = new Employee(112,"Niramal","SSE-1",10001);
Employee employee3 = new Employee(113,"Nijaguna","SSE-2",10);
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
/*for (Employee employee: employeeList){
System.out.println(employee.toString());
}*/
employeeList.stream().forEach(System.out::println);
System.out.println("****");
//employeeList.stream().filter(employee -> employee.salary > 10000 ).forEach(System.out::println);
employeeList.stream().filter(Employee::filterData).map(employee->employee).collect(Collectors.toList());
System.out.println("*******Sorted list*******");
}
}
i'm getting error on below line saying as Bad return type in method reference: cannot convert java.lang.String to boolean
employeeList.stream().filter(Employee::filterData).map(employee->employee).collect(Collectors.toList());
Can someone help on this?? Learning java concepts
it will be helpful if someone help on same
Stream#filter expects a function from the stream type to booleans, and will remove any elements for which that function returns false. The Employee#filterData method returns a String, so it does not work as a function to filter by. Instead, you need a method that returns a boolean: true if the stream should keep that employee, and false if it should not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.