Java 如何从hashmap中按键和值在int-map中的放置顺序获取键和值
可能重复:Java 如何从hashmap中按键和值在int-map中的放置顺序获取键和值,java,hashmap,Java,Hashmap,可能重复: 我使用hashmap存储值,如下所示- Map<String,String> propertyMap=new HashMap<String,String>(); propertyMap.put("Document Type", dataobject.get("dDocType")); propertyMap.put("Document Title", dataobject.get("dDocTitle")); propertyMa
我使用hashmap存储值,如下所示-
Map<String,String> propertyMap=new HashMap<String,String>();
propertyMap.put("Document Type", dataobject.get("dDocType"));
propertyMap.put("Document Title", dataobject.get("dDocTitle"));
propertyMap.put("Revision Label", dataobject.get("dRevLabel"));
propertyMap.put("Security Group", dataobject.get("dSecurityGroup"));
但是当我在列表上迭代时,我没有按照我在映射中的顺序得到键和值
不管怎样,我都可以按照我将其放入地图的顺序获取值。
谢谢propertyMap.entrySet()
您将以Set
的形式获得结果<代码>设置未排序
新建ArrayList(propertyMap.entrySet())代码>按照您在集合中的顺序(可能不是您在映射中的顺序)构造一个列表
如果您正在寻找订单地图,您可以使用
这里有一个有趣的讨论。我相信您正在寻找的是
此链表定义了迭代顺序,通常是将关键帧插入贴图的顺序(插入顺序)
为什么,为什么需要一个单独的ArrayList
?您可以直接迭代Map.entrySet
:
for (final Map.Entry<String, String> entry : propertyMap.entrySet()) {
...
}
for(最终映射。条目:propertyMap.entrySet()){
...
}
您必须使用。有关更多详细信息,请查看Michael给出的问题和答案
for(Map.Entry<String, String> m : map.entrySet()){
// Use m.getKey() to get the Key.
// Use m.getValue() to get the Value.
}
for(Map.Entry m:Map.entrySet()){
//使用m.getKey()获取密钥。
//使用m.getValue()获取值。
}
for(Map.Entry<String, String> m : map.entrySet()){
// Use m.getKey() to get the Key.
// Use m.getValue() to get the Value.
}