java 如何将 HashMap 转换/转换为 LinkedHashMap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38957055/
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
How to convert/cast HashMap to LinkedHashMap?
提问by Emil Adz
I'm using Hawkas a replacement for SharedPreferences
in my application.
我在我的应用程序中使用Hawk作为替代品SharedPreferences
。
I'm trying to store a LinkedHashMap
in it, but for some reason when I pull it back from Hawkit returns as a regular HashMap
and not a LinkedHashMap
. At this point I crash with a ClassCastException
as HashMap
can't be casted to LinkedHashMap
straight forward.
我正在尝试将 a 存储LinkedHashMap
在其中,但是由于某种原因,当我将它从Hawk 中拉回来时,它会作为常规HashMap
而不是LinkedHashMap
. 在这一点上,我崩溃了,ClassCastException
因为HashMap
无法直接投射LinkedHashMap
。
So the question is how can I convert the returned HashMap
to be a LinkedHashMap
?
所以问题是如何将返回的转换HashMap
为 a LinkedHashMap
?
回答by David Wasser
All the answers suggesting that you can create a LinkedHashMap
from a HashMap
are technically correct, but will not give you the desired results :-(
所有建议您可以LinkedHashMap
从 a创建 a 的答案HashMap
在技术上都是正确的,但不会给您想要的结果:-(
Of course, you can create a LinkedHashMap
from a HashMap
, but it isn't guaranteed that the LinkedHashMap
will have the same order that your original did.
当然,您可以LinkedHashMap
从 a创建 a HashMap
,但不能保证LinkedHashMap
将具有与原始顺序相同的顺序。
The problem is that your LinkedHashMap
is serialized when it is stored into the persistent storage as a plain unordered Map
, which does NOT persist the ordering of the individual items. When you then extract the object from the persistent storage, it is returned as a plain HashMap
, and it has lost the "ordering" (which is what you wanted a LinkedHashMap
for in the first place). If you then create a LinkedHashMap
from the returned HashMap
, the ordering will most probably be different from the original.
问题在于,LinkedHashMap
当您将它作为普通 unordered 存储到持久存储中时,它会被序列化Map
,这不会保留单个项目的排序。然后,当您从持久存储中提取对象时,它会作为一个普通的 返回HashMap
,并且它已经失去了“排序”(这是您首先想要的LinkedHashMap
)。如果您随后LinkedHashMap
从返回的 中创建 a HashMap
,则排序很可能与原始排序不同。
In order to do this correctly, you should probably convert your LinkedHashMap
into an ordered array of objects and store this ordered array in the persistent storage. You can then read the ordered array of objects back from the persistent storage and recreate the LinkedHashMap
with the correct order. Basically, you need to serialize and deserialize the LinkedHashMap
yourself.
为了正确地做到这一点,您可能应该将您LinkedHashMap
的对象转换为一个有序的对象数组,并将这个有序的数组存储在持久存储中。然后,您可以从持久存储中读取有序的对象数组,并LinkedHashMap
以正确的顺序重新创建对象。基本上,您需要自己序列化和反序列化LinkedHashMap
。
See my answer to this questionfor more details.
有关更多详细信息,请参阅我对这个问题的回答。
回答by Nick
Just create a new LinkedHashMap, since it can take any Map as a constructor argument.
只需创建一个新的 LinkedHashMap,因为它可以将任何 Map 作为构造函数参数。
LinkedHashMap<Object> newMap = new LinkedHashMap<>(theHashMapReturnedFromHawk);
Object
would be the type you need.
Object
将是您需要的类型。
回答by Pankaj Singhal
One of LinkedHashMap's constructors accepts a Map
. It will return a LinkedHashMap
with the same content as HashMap
.
其中之一的LinkedHashMap的构造函数接受Map
。它将返回LinkedHashMap
与HashMap
.
Code sample:
代码示例:
LinkedHashMap<T> newMap = new LinkedHashMap<T>(hashmap);
where T
is the type of the objects stored in the HashMap
其中T
存储的对象的类型是HashMap