In our Web application project, we are using the Redis to manage the session. To support it, we are serializing any object which will be stored in the session.
For example, we use DTO's to hold the bean data which is used to display on the screen. Even if the DTO having any other object inside (Composition) we also have to serialize it otherwise, we get NotSerializableException
.
I had a problem when I was creating an anonymous inner class to implement the Comparator
like below:
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getLastName().compareTo(p2.getLastName());
}
});
The above code threw the NotSerializableException
and I resolved it by creating a class that implements the Comparator
as well as Serializable
interface. The problem was, it was thrown inside the JSP
page which was using this DTO. I had to do a lot of debugging to find the actual problem.
But now, I'm wondering to change the above code to use Lambda expression like below:
Collections.sort(people, (p1, p2) -> p1.getLastName().compareTo(p2.getLastName()));
However, I fear the same exception might occur. Does the Lambda expression create objects internally?