gpt4 book ai didi

java - Java中Generic Type序列化的HashMap之HashMap

转载 作者:行者123 更新时间:2023-11-30 08:27:42 25 4
gpt4 key购买 nike

已修复,根据 Engineer Dollery 的回答,解决方案在底部。

目标是序列化网络类的网络字段。抱歉命名。

我有这个,可以正确编译和序列化。

public class Network {
private HashMap<String, HashMap<String, Number>> network;
...
public void load(String networkFile) throws Exception{
network = (HashMap<String, HashMap<String, Number>>)Utility.deserialize(Utility.load(networkFile));
}

public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}

class Number implements Serializable {
...
}

我将其更改为这个通用版本,它可以编译,但不再序列化。我需要以某种方式指定 T 和 Number 的 HashMap 是可序列化的。

public class Network<T extends Serializable> {
private HashMap<T, HashMap<T, Number>> network;
...

public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}

public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}

class Number implements Serializable {
...
}

这是序列化的代码

package Bullib;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Utility{

public static Pattern phrasePattern = Pattern.compile("[^;:!.?]+");
public static Pattern wordPattern = Pattern.compile("['A-Za-z]+");

public static Object deserialize(byte[] serialized) throws Exception {
ByteArrayInputStream bi = new ByteArrayInputStream(serialized);
ObjectInputStream si = new ObjectInputStream(bi);
return si.readObject();
}

public static byte[] serialize(Object target) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(target);
so.flush();
return bo.toByteArray();
}

public static byte[] load(String filename) throws Exception {
RandomAccessFile file = new RandomAccessFile(new File(filename), "r");
byte[] b = new byte[(int)file.length()];
file.read(b);
file.close();
return b;
}

public static void save(String filename, byte[] value) throws Exception {
RandomAccessFile file = new RandomAccessFile(new File(filename), "rw");
file.write(value);
file.close();
}

public static Collection<String> executeRegex(Pattern pattern, String text){
LinkedList<String> matches = new LinkedList<String>();
Matcher m = pattern.matcher(text);
while (m.find()) {
matches.add(m.group(0));
}
return matches;
}
}

这是我尝试将 T 序列化为 StringDouble 时返回的错误

Exception in thread "main" java.io.NotSerializableException: Bullib.Network.Netw
ork
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Bullib.Utility.serialize(Utility.java:21)
at Bullib.Network.Network.save(Network.java:88)
at Engine.save(Engine.java:63)
at Engine.main(Engine.java:88)

根据请求,示例测试和错误。如您所见,它会产生与上述相同的异常。我已经更新了测试,以表明如果没有向 map 添加任何内容,它就会通过,否则会失败。

public static void main(String[] args) throws Exception{
Network<String> stringtest = new Network<String>("","","");
stringtest.save("testempty");
stringtest.load("testempty");
System.out.println("passed empty");
stringtest.placeAndMove("fill",0.0);
stringtest.save("testfilled");
stringtest.load("testfilled");
System.out.println("passed filled");
}

java: Bullib/Network/Network
passed empty
Exception in thread "main" java.io.NotSerializableException: Bullib.Network.Netw
ork
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Bullib.Utility.serialize(Utility.java:21)
at Bullib.Network.Network.save(Network.java:88)
at Bullib.Network.Network.main(Network.java:108)

解决办法是改变

public class Network<T extends Serializable> {
private HashMap<T, HashMap<T, Number>> network;
...

public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}

public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}

class Number implements Serializable {
...
}

public class Network<T extends Serializable> implements Serializable {
private HashMap<T, HashMap<T, Number>> network;
...

public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}

public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}

class Number implements Serializable {
...
}

最佳答案

试试这个:

public class Network implements Serializable

关于java - Java中Generic Type序列化的HashMap之HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20713107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com