序列化/反序列化 LinkedHashMap (android) java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2890346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
serialize/deserialize a LinkedHashMap (android) java
提问by weakwire
So i want to pass a LinkedHashMap to an intent.
所以我想将 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");
This one Worked Like a charm with HashMap. But with LinkedHashMap i got a problem i got a runtime error here
这个使用 HashMap 就像一个魅力。但是使用 LinkedHashMap 我遇到了一个问题 我在这里遇到了运行时错误
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");
I get no error with HashMap.
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
我也收到了一个警告“类型安全:未检查从 Serializable 到 LinkedHashMap 的强制转换”但是我也收到了 HashMap 的这个警告。任何想法。非常感谢任何帮助
Also I just saw this. https://issues.apache.org/jira/browse/HARMONY-6498
采纳答案by Stephen C
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 Stringinstances.
泛型类型的运行时类型是原始类型;即类型参数的实际类型未知的类型。在这种情况下,运行时类型将为LinkedHashMap<?, ?>. 这不能安全地转换为类型,LinkedMashMap<String, String>因为运行时系统无法知道所有的键和值实际上都是String实例。
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 maylater get a
ClassCastExceptionat a totally unexpected place; e.g. while iterating the keys or assigning the result of aget.You could manually copy the deserialised
Map<?, ?>into a newLinkedMashMap<String, String>, explicitly casting each key and value toString.
您可以添加一个注释来告诉编译器“关闭”未经检查的强制转换。这有点冒险。如果由于某种原因,其中一个键或值实际上不是字符串,那么您的应用程序稍后可能会
ClassCastException在一个完全出乎意料的地方得到 ;例如,在迭代键或分配 a 的结果时get。在deserialised你可以手动复制
Map<?, ?>到一个新的LinkedMashMap<String, String>,明确铸造每个键和值String。
UPDATE
更新
Apparently the real cause of the runtime exception (as distinct from the "unsafe typecast" compilation error) is that Android is passing the serializedExtrabetween intents as a regular HashMaprather than using the Mapclass that you provided. This is described here:
显然,运行时异常的真正原因(不同于“不安全的类型转换”编译错误)是 Android 将serializedExtra意图之间作为常规传递,HashMap而不是使用Map您提供的类。这是在此处描述的:
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.
正如问答所解释的那样,没有简单的解决方法。如果要传递保留键顺序的映射,则必须将映射转换为成对数组并传递它。另一方面,您将需要从传递的数组中重建映射。
回答by miz
LinkedHashMap does implement Map interface, here is definition from the source code:
LinkedHashMap 确实实现了 Map 接口,这里是源代码中的定义:
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.
以下测试工作正常,所以我认为您遇到的问题与 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);

