您如何序列化番石榴集合? [英] How do you serialize a guava collection?

查看:96
本文介绍了您如何序列化番石榴集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定缺少明显的东西,但是我无法序列化 TreeBasedTable 。它被标记为 @GwtCompatible(serializable = true),所以我的理解是我需要使用 guava-gwt

I must be missing something obvious but I can't manage to serialize a TreeBasedTable. It is marked as @GwtCompatible(serializable = true), so my understanding is that I need to use the guava-gwt library to (de-)serialize it.

但我找不到这样做。

有关信息,我的pom包含番石榴 guava-gwt ,均为14.0版。

For information, my pom contains guava and guava-gwt, both version 14.0.

编辑

因此,感谢您的回答,我现在知道TreeBasedTable是可序列化的。因此,我删除了所有gwt引用并使其正常工作。但是,此代码仍然失败(这是使我认为TreeBasedTable无法序列化的代码)-所以我想问题出在自定义比较器...

So thanks to the answer, I now understand that TreeBasedTable is serializable. So I have removed all the gwt references and got it to work. However, this code still fails (which is the code that made me think that TreeBasedTable was not serializable) - so I guess the problem is with the custom Comparators...

public static void main(String[] args) throws Exception {
    //with the following table it works
    //Table<Integer, String, Object> table = TreeBasedTable.create();

    //but with this one, it fails
    Table<Integer, String, Object> table = TreeBasedTable.create(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }, String.CASE_INSENSITIVE_ORDER);

    table.put(1, "s", 123);

    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Paths.get("c:/temp/test").
            toFile()));) {
        oos.writeObject(table);
    }

    Table<Integer, String, Object> saved = null;

    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Paths.get("c:/temp/test").
            toFile()));) {
        saved = (Table<Integer, String, Object>) ois.readObject();
    }

    System.out.println(table.equals(saved));
    Files.delete(Paths.get("C:/temp/test"));
}


推荐答案

是Java序列化,是的: TreeBasedTable Serializable

If you are talking about plain Java serialization, yes: TreeBasedTable is Serializable.

TreeBasedTable 扩展了 StandardRowSortedTable ,扩展了 StandardTable ,其实现了可序列化

TreeBasedTable extends StandardRowSortedTable which extends StandardTable which implements Serializable.

顺便说一句:简单的支票可以帮助您:

BTW: a simple check would have helped you:

Serializable foo = TreeBasedTable.create();

由于编译器没有抱怨这一行,因此您知道 TreeBasedTable 实现可序列化

Since the compiler doesn't complain about this line, you know that TreeBasedTable implements Serializable.

更新:

String.CASE_INSENSITIVE_ORDER 实现可序列化,因此您所需要做的就是将匿名Integer比较器重构为实现 Serializable 的内部类。或者:更好,使用 Ordering.natural() ,它已经是可序列化

String.CASE_INSENSITIVE_ORDER implements Serializable, so all you need to do is to refactor your anonymous Integer comparator into an inner class that implements Serializable. Or: better yet, use Ordering.natural() instead, which is Serializable already.

这篇关于您如何序列化番石榴集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆