本文介绍了序列化的lambda和没有serialVersionUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试学习序列化如何与Java及其最新版本一起使用。我正在尝试序列化这样的lambda:I'm trying to learn how the serialization works with Java and its lastest version. I'm trying to serialize a lambda like this :Runnable r = (Runnable & Serializable)() -> {System.out.println("This is a test");};但我注意到我没有关于缺少 serialVersionUID 变量。这是正常的吗?But I notice that I have no warning about the absence of a serialVersionUID variable. Is it normal ?我知道它会在运行时生成,但强烈建议定义它: https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.htmlI know it will be generated at the runtime however it is strongly recommended to define it : https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html我该怎么办?如何在我的Lambda中定义它?What should I do ? How can I define it in my Lambda ?谢谢推荐答案 serialVersionUID 仅与生成流标识符。如果可序列化类具有 writeReplace()方法,则不是这种情况(也在 Serializable 文档)返回另一个类的替换对象因为这样的表示与原始类完全分离。这是可序列化的lambda实例所发生的情况,请参阅 SerializedLambda :The serialVersionUID is only relevant to classes which generate a stream identifier. This is not the case if the serializable class has a writeReplace() method (also described in the Serializable documentation) that returns a substitute object of a different class, as such a representation is fully decoupled from the original class. This is what happens with serializable lambda instances, see SerializedLambda:所以这是一个 SerializedLambda 的实例,最后在流上因此,该类有责任拥有稳定的序列化表示。不幸的是,这并不能保护您免受可能的不兼容性的影响。So it’s an instance of SerializedLambda that ends up on the stream and thus the responsibility of that class to have a stable serialized representation. Unfortunately that doesn’t protect you from possible incompatibilities.反序列化后,将调用定义lambda表达式的类的合成方法(与这个和这个答案)拒绝与该类中lambda表达式的现有定义不匹配的反序列化尝试,而匹配可能取决于lambda定义的细微方面。请注意,即使使用Eclipse而不是 javac 重新编译定义类也可能会破坏序列化兼容性。Upon deserialization, a synthetic method of the class defining the lambda expression will get called (compare to this and this answer) which will reject deserialization attempts which do not match an existing definition of a lambda expression within that class, whereas the matching may depend on subtle aspects of the lambda’s definition. Note that even recompiling the defining class with Eclipse rather than javac might break the Serialization compatibility.不是 Serializable lambdas 的安全影响。一般来说,我建议避免使用它。Not also the security impacts of Serializable lambdas. Generally, I recommend to avoid using it. 这篇关于序列化的lambda和没有serialVersionUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!