Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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 - Fatal编程技术网

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
}