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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:56:36  来源:igfitidea点击:

How to convert/cast HashMap to LinkedHashMap?

javaandroidcastinghashmaplinkedhashmap

提问by Emil Adz

I'm using Hawkas a replacement for SharedPreferencesin my application.

我在我的应用程序中使用Hawk作为替代品SharedPreferences

I'm trying to store a LinkedHashMapin it, but for some reason when I pull it back from Hawkit returns as a regular HashMapand not a LinkedHashMap. At this point I crash with a ClassCastExceptionas HashMapcan't be casted to LinkedHashMapstraight forward.

我正在尝试将 a 存储LinkedHashMap在其中,但是由于某种原因,当我将它从Hawk 中拉回来时,它会作为常规HashMap而不是LinkedHashMap. 在这一点上,我崩溃了,ClassCastException因为HashMap无法直接投射LinkedHashMap

So the question is how can I convert the returned HashMapto be a LinkedHashMap?

所以问题是如何将返回的转换HashMap为 a LinkedHashMap

回答by David Wasser

All the answers suggesting that you can create a LinkedHashMapfrom a HashMapare technically correct, but will not give you the desired results :-(

所有建议您可以LinkedHashMap从 a创建 a 的答案HashMap在技​​术上都是正确的,但不会给您想要的结果:-(

Of course, you can create a LinkedHashMapfrom a HashMap, but it isn't guaranteed that the LinkedHashMapwill have the same order that your original did.

当然,您可以LinkedHashMap从 a创建 a HashMap,但不能保证LinkedHashMap将具有与原始顺序相同的顺序。

The problem is that your LinkedHashMapis 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 LinkedHashMapfor in the first place). If you then create a LinkedHashMapfrom 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 LinkedHashMapinto 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 LinkedHashMapwith the correct order. Basically, you need to serialize and deserialize the LinkedHashMapyourself.

为了正确地做到这一点,您可能应该将您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);

Objectwould be the type you need.

Object将是您需要的类型。

回答by Pankaj Singhal

One of LinkedHashMap's constructors accepts a Map. It will return a LinkedHashMapwith the same content as HashMap.

其中之一的LinkedHashMap的构造函数接受Map。它将返回LinkedHashMapHashMap.

Code sample:

代码示例:

LinkedHashMap<T> newMap = new LinkedHashMap<T>(hashmap);

where Tis the type of the objects stored in the HashMap

其中T存储的对象的类型是HashMap