Errors
Non-static method βmethod(java.lang.String)β cannot be referenced from a static context
- You canβt call something that doesnβt exist. Since you havenβt created an object, the non-static method doesnβt exist yet. A static method (by defination) always exists.
- EX:
public class EmployeeSimple {
private String name;
private Integer age;
private Double salary;
private char level;
private int experience;
}
- EmployeeSimple class is of type EmployeeSimple
int minAge = employees.stream()
.filter(Objects::nonNull).filter(emp -> null != emp.getAge()).filter(emp -> null != emp.getName())
.map(EmployeeSimple::getAge)
.min(Comparator.comparing(EmployeeSimple::getSalary))
- Here
min(Comparator.comparing(EmployeeSimple::getSalary)) gives this compile time error because in the above line since we performed mapping on EmployeeSimple object where EmployeeSimple object changed its form from EmployeeSimple to age which is an Integer. Now EmployeeSimple no longer exsists to get any field from employeeSimple.
int minAge = employees.stream()
.filter(Objects::nonNull).filter(emp -> null != emp.getAge()).filter(emp -> null != emp.getName())
.map(EmployeeSimple::getAge)
.min(Comparator.comparing(Integer::intValue))
- Here we are finally getting an integer value
Add exception to method signature
- Exception in method using throws keyword which extends Exception β> As custom exceptions are always uncheckedExceptions.
if stream<T>
- Donβt have to use collect(collectors.toList) after stream since the dataType is expecting as stream not as a list.