哈希集序列化/克隆问题

ds97pgxw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(323)

为什么会失败?我在比较一个空的 HashSet 其克隆的序列化形式由序列化+反序列化创建。

import org.apache.commons.lang3.SerializationUtils;
import static org.assertj.core.api.Assertions.assertThat;

...

final HashSet<String> hashSet = new HashSet<>();
assertThat(SerializationUtils.serialize(hashSet))
    .containsExactly(SerializationUtils.serialize(SerializationUtils.clone(hashSet)));

将抛出assertionerror。
哈希集中的某些内容不能正确序列化/反序列化。你知道具体是什么吗?我使用的是openjdk8。

pvcm50d1

pvcm50d11#

它和载荷因子有关。如果哈希集是这样示例化的,那么它可以工作:

new HashSet(0,0.75f);

相关问题