序列化/反序列化 LinkedHashMap (android) java

发布于 2024-09-02 12:48:28 字数 855 浏览 8 评论 0原文

所以我想将 LinkedHashMap 传递给意图。

//SEND THE MAP
Intent singlechannel = new Intent(getBaseContext(),singlechannel.class);
singlechannel.putExtra("db",shows1);//perase to
startActivity(singlechannel);

//GET THE MAP
LinkedHashMap<String,String> db = new LinkedHashMap<String,String>();   
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

这个与 HashMap 一起工作就像一个魅力。 但是使用 LinkedHashMap 我遇到了一个问题,我在这里遇到运行时错误,但

  db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

HashMap 没有错误。

我还收到警告“类型安全:从 Serialized 到 LinkedHashMap 的未经检查的转换” 但我对 HashMap 也有这个警告。 任何想法。任何帮助我都非常感激

我也刚刚看到这个。 https://issues.apache.org/jira/browse/HARMONY-6498

So i want to pass a LinkedHashMap to an intent.

//SEND THE MAP
Intent singlechannel = new Intent(getBaseContext(),singlechannel.class);
singlechannel.putExtra("db",shows1);//perase to
startActivity(singlechannel);

//GET THE MAP
LinkedHashMap<String,String> db = new LinkedHashMap<String,String>();   
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

This one Worked Like a charm with HashMap.
But with LinkedHashMap i got a problem i got a runtime error here

  db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

I get no error with HashMap.

I also got a warning "Type safety: Unchecked cast from Serializable to LinkedHashMap"
But i had this warning with HashMap too.
Any ideas.Any help is much appreciated

Also I just saw this.
https://issues.apache.org/jira/browse/HARMONY-6498

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-09-09 12:48:28

这里问题的根源是您试图将类型转换为泛型类型。如果没有不安全/未经检查的类型转换,这是无法完成的。

泛型类型的运行时类型是原始类型;即类型参数的实际类型未知的类型。在这种情况下,运行时类型将为 LinkedHashMap。这不能安全地类型转换为 LinkedMashMap,因为运行时系统无法知道所有键和值实际上都是 String 实例。

您有两个选择:

  • 您可以添加注释来告诉编译器“关闭”未经检查的强制转换。这有点冒险。如果由于某种原因,其中一个键或值实际上不是字符串,您的应用程序可能稍后会在完全意想不到的地方收到 ClassCastException ;例如,在迭代键或分配 get 的结果时。

  • 您可以手动将反序列化的 Map 复制到新的 LinkedMashMap 中,将每个键和值显式转换为 String

更新

显然,运行时异常的真正原因(与“不安全类型转换”编译错误不同)是 Android 将 serializedExtra 作为常规 在意图之间传递HashMap 而不是使用您提供的 Map 类。此处对此进行了描述:

正如问答所解释的那样,没有简单的解决方法。如果您想传递保留键顺序的映射,则必须将映射转换为对数组并传递它。另一方面,您需要根据传递的数组重建地图。

The root of the problem here is that you are trying to type cast to a generic type. This cannot be done without an unsafe/unchecked type cast.

The runtime types of generic types are raw types; i.e. types in which the actual types of the type parameters are not known. In this case the runtime type will be LinkedHashMap<?, ?>. This cannot be safely typecast to LinkedMashMap<String, String> because the runtime system has no way of knowing that all of the keys and values are actually String instances.

You have two options:

  • You could add an annotation to tell the compiler to "shut up" about the unchecked cast. This is a bit risky. If for some reason one of the keys or values is not actually a String, your application may later get a ClassCastException at a totally unexpected place; e.g. while iterating the keys or assigning the result of a get.

  • You could manually copy the deserialised Map<?, ?> into a new LinkedMashMap<String, String>, explicitly casting each key and value to String.

UPDATE

Apparently the real cause of the runtime exception (as distinct from the "unsafe typecast" compilation error) is that Android is passing the serializedExtra between intents as a regular HashMap rather than using the Map class that you provided. This is described here:

As that Q&A explains, there is no simple workaround. If you want to pass a map preserving the key order, will have to convert the map to an array of pairs and pass that. On the other end, you will then need to reconstruct the map from the passed array.

噩梦成真你也成魔 2024-09-09 12:48:28

LinkedHashMap确实实现了Map接口,这里是源代码中的定义:

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>

以下测试工作正常,所以我认为您遇到的问题与LinkedHashMap无关。

Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("1", "one");
        FileOutputStream fos = new FileOutputStream("c://map.ser");
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(map);
        out.close();


        FileInputStream fin = new FileInputStream("c://map.ser");
        ObjectInputStream in = new ObjectInputStream(fin);
        Map<String, String> map2 = (Map<String, String>) in.readObject();
        System.out.println(map2); 

LinkedHashMap does implement Map interface, here is definition from the source code:

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>

The following test is working fine, so I think the problem you have is not related LinkedHashMap.

Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("1", "one");
        FileOutputStream fos = new FileOutputStream("c://map.ser");
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(map);
        out.close();


        FileInputStream fin = new FileInputStream("c://map.ser");
        ObjectInputStream in = new ObjectInputStream(fin);
        Map<String, String> map2 = (Map<String, String>) in.readObject();
        System.out.println(map2); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文