gpt4 book ai didi

java - Map.keySet() 读取序列?

转载 作者:太空狗 更新时间:2023-10-29 13:49:54 25 4
gpt4 key购买 nike

我有 Map<String, List<String>> map现在,将两个值放在 Map 中,现在当从 map 读取键集时,在不同的设备中以不同的顺序给出。

private Map<String, List<String>>  map = new HashMap<>();

map.put("First", new ArrayList<String>());
map.put("Second", new ArrayList<String>());

现在,从这张 map 中读取 key 。

 Set<String> keys = map.keySet();

for (int i = 0; i < map.size(); i++) {
Log.d("key", keys.toArray()[i].toString());
}

OREO 8.0 的输出

D/Key : First
D/Key : Second

低于 8.0 的输出

D/Key : Second
D/Key : First

最佳答案

在我们将 Set 定义为 SortedSet 之前,我得到了一个解决方案,我们以不同的顺序获得键序列。

SortedSet<String> keys =  new TreeSet<>(map.keySet());

for (int i = 0; i < map.size(); i++) {
Log.d("key", keys.toArray()[i].toString());
}

关于java - Map.keySet() 读取序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com