There is a Class Role, having data member as String roleName. I have to sort a list of Role.
While sorting I need to check NPE and trim roleName also. So I used
roleList.sort(Comparator.comparing(Role::getRoleName,
Comparator.nullsLast(Comparator.naturalOrder())));
I can suppress the null pointer exception but can't use trim.
roleList.sort( (x, y) -> x.getRole().trim().compareTo(y.getRole().trim()));
Using this I can't avoid NPE.