您如何在 Java 中外部化列表
How do you Externalize a List in Java
我有一个 class 实现了 Externalizable,它包含三个对象和这三种对象类型之一的列表。我假设您将简单地在子 classes 中实现 externalizable,然后为成员对象定义外部读写,但是否也可以包含 List,如果可以的话如何?
public class AuthSuccessPacket extends Packet implements Externalizable {
public static final long serialVersionUID = 10003L;
Contact contact;
Network network;
Account account;
List<Contact> contacts;
public AuthSuccessPacket(){
super(Type.AUTH_SUCCESS);
}
public AuthSuccessPacket(Contact contact, Network network, Account account, List<Contact> contacts){
super(Type.AUTH_SUCCESS);
this.contact = contact;
this.network = network;
this.account = account;
this.contacts = contacts;
}
@Override
public void writeExternal(ObjectOutput out) {
out.writeObject(account);
out.writeObject(network);
out.writeObject(contact);
// ??? write list
}
@Override
public void readExternal(ObjectInput in) {
}
contact = (Contact) in.readObject();
network = (Network) in.readObject();
account = (Account) in.readObject();
// ??? read list
}
我会尝试类似的方法:
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(account);
out.writeObject(network);
out.writeObject(contact);
// ??? write list
out.writeInt(contacts.size());
for (Contact cntct : contacts) {
out.writeObject(cntct);
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
contact = (Contact) in.readObject();
network = (Network) in.readObject();
account = (Account) in.readObject();
// ??? read list
contacts = new ArrayList<>();
int contactsNo = (int) in.readInt();
for (int i = 0; i < contactsNo; i++) {
Contact cntct = (Contact) in.readObject();
contacts.add(cntct);
}
}
public static class Person implements Externalizable{
private String firstName;
private String lastName;
private int age;
private Person mother;
private Person father;
private List<Person> children;
public Person(){
}
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public void setMother(Person mother) {
this.mother = mother;
}
public void setFather(Person father) {
this.father = father;
}
public void setChildren(List<Person> children) {
this.children = children;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(mother);
out.writeObject(father);
out.writeObject(firstName);
out.writeObject(lastName);
out.writeInt(age);
out.writeObject(children);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
mother = (Person) in.readObject();
father = (Person) in.readObject();
firstName = (String)in.readObject();
lastName = (String) in.readObject();
age = in.readInt();
children = (List<Person>) in.readObject();
}
}
我有一个 class 实现了 Externalizable,它包含三个对象和这三种对象类型之一的列表。我假设您将简单地在子 classes 中实现 externalizable,然后为成员对象定义外部读写,但是否也可以包含 List,如果可以的话如何?
public class AuthSuccessPacket extends Packet implements Externalizable {
public static final long serialVersionUID = 10003L;
Contact contact;
Network network;
Account account;
List<Contact> contacts;
public AuthSuccessPacket(){
super(Type.AUTH_SUCCESS);
}
public AuthSuccessPacket(Contact contact, Network network, Account account, List<Contact> contacts){
super(Type.AUTH_SUCCESS);
this.contact = contact;
this.network = network;
this.account = account;
this.contacts = contacts;
}
@Override
public void writeExternal(ObjectOutput out) {
out.writeObject(account);
out.writeObject(network);
out.writeObject(contact);
// ??? write list
}
@Override
public void readExternal(ObjectInput in) {
}
contact = (Contact) in.readObject();
network = (Network) in.readObject();
account = (Account) in.readObject();
// ??? read list
}
我会尝试类似的方法:
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(account);
out.writeObject(network);
out.writeObject(contact);
// ??? write list
out.writeInt(contacts.size());
for (Contact cntct : contacts) {
out.writeObject(cntct);
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
contact = (Contact) in.readObject();
network = (Network) in.readObject();
account = (Account) in.readObject();
// ??? read list
contacts = new ArrayList<>();
int contactsNo = (int) in.readInt();
for (int i = 0; i < contactsNo; i++) {
Contact cntct = (Contact) in.readObject();
contacts.add(cntct);
}
}
public static class Person implements Externalizable{
private String firstName;
private String lastName;
private int age;
private Person mother;
private Person father;
private List<Person> children;
public Person(){
}
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public void setMother(Person mother) {
this.mother = mother;
}
public void setFather(Person father) {
this.father = father;
}
public void setChildren(List<Person> children) {
this.children = children;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(mother);
out.writeObject(father);
out.writeObject(firstName);
out.writeObject(lastName);
out.writeInt(age);
out.writeObject(children);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
mother = (Person) in.readObject();
father = (Person) in.readObject();
firstName = (String)in.readObject();
lastName = (String) in.readObject();
age = in.readInt();
children = (List<Person>) in.readObject();
}
}