Java hashmap的访问值
可能重复:Java hashmap的访问值,java,android,Java,Android,可能重复: 我有一个映射,MAP-MAP=newhashmap() 我已经实现了map的方法,现在请告诉我如何访问hashmap中存在的所有值。我需要在android的UI上显示它们 map.values()为您提供一个集合,其中包含HashMap中的所有值。如果您准备好了HashMap数据,那么您只需迭代HashMap键。只需执行一次迭代并逐个获取数据 选中此项:如果您想从哈希映射中并行访问键和值,则可以使用方法:- Map<String, Records> map = new
我有一个映射,
MAP-MAP=newhashmap()代码>
我已经实现了map的方法,现在请告诉我如何访问hashmap中存在的所有值。我需要在android的UI上显示它们 map.values()
为您提供一个集合
,其中包含HashMap
中的所有值。如果您准备好了HashMap
数据,那么您只需迭代HashMap
键。只需执行一次迭代并逐个获取数据
选中此项:如果您想从哈希映射中并行访问键
和值
,则可以使用方法:-
Map<String, Records> map = new HashMap<String, Records> ();
//Populate HashMap
for(Map.Entry<String, Record> entry: map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
有关更多详细说明,请参阅本文:-您可以使用For循环来完成
Set keys = map.keySet(); // It will return you all the keys in Map in the form of the Set
for (Iterator i = keys.iterator(); i.hasNext();)
{
String key = (String) i.next();
Records value = (Records) map.get(key); // Here is an Individual Record in your HashMap
}
你遇到了什么错误?您应该能够通过为记录分配的键来获取记录,或者可以通过foreach循环对其进行迭代。是否有任何方法可以按字母顺序对键进行排序?@onkar。对于这种情况,可以使用TreeMap
。见此帖:-
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
for(Map.Entry<String, Integer> entry: treeMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
Set keys = map.keySet(); // It will return you all the keys in Map in the form of the Set
for (Iterator i = keys.iterator(); i.hasNext();)
{
String key = (String) i.next();
Records value = (Records) map.get(key); // Here is an Individual Record in your HashMap
}