【问题标题】:Cannot locate item in list after serialization序列化后无法在列表中找到项目
【发布时间】:2019-05-04 12:55:09
【问题描述】:

首先我创建项目 A。然后我将项目 A 添加到列表 A。然后我将项目 A 序列化到一个文件中,然后我从同一个文件中读取项目 A。最后,我从列表 A 中找到读取的项目 B。在列表 A 中定位项目 B 失败。 println 语句的结果是-1。我的问题是为什么?

public class ListTest {

    public static void main(String[] args) {

        Item a = new Item("a");
        List<Item> listA = new ArrayList<Item>();
        listA.add(a);

        try (FileOutputStream fos = new FileOutputStream(new File("text.dat"));
            ObjectOutputStream oos = new ObjectOutputStream(fos)) {         
            oos.writeObject(a);         

            try (FileInputStream fis = new FileInputStream(new File("text.dat"));
                    ObjectInputStream ois = new ObjectInputStream(fis)) {
                        Item b = (Item) ois.readObject();           
                        System.out.println(listA.indexOf(b));           
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();                    
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

** 编辑 1 **

商品的代码在哪里。

public class Item implements Serializable {

    private static final long serialVersionUID = 1L;

    private String mName;

    public Item(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }
}

【问题讨论】:

  • Item 是否实现了Serializable
  • 是的,确实如此。我添加了编辑 1 的代码。

标签: java file arraylist serialization


【解决方案1】:

您的代码有 2 个问题:

  1. 要检查列表中是否存在元素,您需要使用.contains() 更改此

    listA.indexOf(b);
    

    listA.contains(b);
    

    来自docs,

    boolean contains(Object o)

    如果此列表包含指定元素,则返回 true。更多的 正式地,当且仅当此列表包含至少一个时才返回 true 元素 e 使得 (o==null ? e==null : o.equals(e))。

  2. 现在,由于您正在检查您需要的对象的相等性 覆盖 Item 类中的 equals()hashcode()。将以下方法添加到Item

    class Item implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private String mName;
    
        // getters and setters
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((mName == null) ? 0 : mName.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Item other = (Item) obj;
            if (mName == null) {
                if (other.mName != null)
                    return false;
            } else if (!mName.equals(other.mName))
                return false;
            return true;
        }
    
    }   
    

    现在您知道,如果对象的所有属性都具有相同的值,则两个对象是相等的。如果您现在检查contains(),它将返回true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多