Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在HashMap中用我自己的键排列现有键?_Java_Android_Arraylist_Hashmap - Fatal编程技术网

Java 如何在HashMap中用我自己的键排列现有键?

Java 如何在HashMap中用我自己的键排列现有键?,java,android,arraylist,hashmap,Java,Android,Arraylist,Hashmap,我有一个HashMap,其中键表示类模型的顺序。众所周知,哈希映射在检索或输出时并不保持键的顺序。我还有一个ArrayList,它存储类模型的预期排列顺序。例如,当我从Hashmap中检索键值时,我会得到类似于car1、car4、car3、car5和car2的内容。相反,ArrayList值具有排列值的预期顺序;1号车,2号车,3号车,4号车和5号车。我想根据ArrayList中的键来订购。我的代码如下: for (HashMap.Entry<String, FloorPlanMo

我有一个HashMap,其中键表示类模型的顺序。众所周知,哈希映射在检索或输出时并不保持键的顺序。我还有一个ArrayList,它存储类模型的预期排列顺序。例如,当我从Hashmap中检索键值时,我会得到类似于car1、car4、car3、car5和car2的内容。相反,ArrayList值具有排列值的预期顺序;1号车,2号车,3号车,4号车和5号车。我想根据ArrayList中的键来订购。我的代码如下:

    for (HashMap.Entry<String, FloorPlanModel> existingKeys : floorPlanKeys.entrySet()) {
        Log.d(LOG_TAG, "existingKeys entries = " + existingKeys.getKey());
    }
    for (String newKeys : buildingModel.getFpKeys()) {
        Log.d(LOG_TAG, "newKeys entries = " + newKeys);
    }
我应该将第二个for循环放在第一个for循环中,但是如何迭代HashMap中的每个值并将它们与ArrayList中的值排序呢?如果我覆盖它们,hashmap中的值会改变吗


我忘了说我不能用树形图来做这个。我需要手动安排。

是你的朋友。带有排序/可排序键的地图。

是您的朋友。带有排序/可排序键的映射。

好的,我忘了提到我目前无法使用树形映射,因为我从Firebase检索数据(它只将数据存储在HashMaps中)并直接将其输入到ExpandableListAdapter中(我必须将值插入到子级和组中,该组以整数形式占据位置). 因此,我将迭代它们并手动重新排列它们。
new TreeMap(myHashMap)
将对其进行转换。好吧,我忘了提到,由于从Firebase检索数据(只将数据存储在hashmap中)并将其直接输入ExpandableListAdapter,我目前无法使用TreeMaps(因此,我必须将值插入到子级和组中,该子级和组以整数表示)。因此,我将迭代它们并手动重新排列它们。
new TreeMap(myHashMap)
将对其进行转换。
existingKeys entries = floorplan0
existingKeys entries = floorplan2
existingKeys entries = floorplan3
existingKeys entries = floorplan4
existingKeys entries = floorplan1

newKeys entries = floorplan0
newKeys entries = floorplan1
newKeys entries = floorplan2
newKeys entries = floorplan3
newKeys entries = floorplan4