Android-从 HashMap 获取值

我试过在 Android 中搜索 HashMap,但是遇到了问题:

考虑一下这个例子:

HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");

现在我想迭代它,并得到每种颜色的值,并希望在“吐司”中显示。 我该怎么展示它?

197667 次浏览
Iterator myVeryOwnIterator = meMap.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
String key=(String)myVeryOwnIterator.next();
String value=(String)meMap.get(key);
Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");
Iterator iterator = meMap.keySet().iterator();
while( iterator. hasNext() ){
Toast.makeText(getBaseContext(), meMap.get(iterator.next().toString()),
Toast.LENGTH_SHORT).show();
}
HashMap<String, String> meMap = new HashMap<String, String>();
meMap.put("Color1", "Red");
meMap.put("Color2", "Blue");
meMap.put("Color3", "Green");
meMap.put("Color4", "White");


Iterator myVeryOwnIterator = meMap.values().iterator();
while(myVeryOwnIterator.hasNext()) {
Toast.makeText(getBaseContext(), myVeryOwnIterator.next(), Toast.LENGTH_SHORT).show();
}

Here's a simple example to demonstrate Map usage:

Map<String, String> map = new HashMap<String, String>();
map.put("Color1","Red");
map.put("Color2","Blue");
map.put("Color3","Green");
map.put("Color4","White");


System.out.println(map);
// {Color4=White, Color3=Green, Color1=Red, Color2=Blue}


System.out.println(map.get("Color2")); // Blue


System.out.println(map.keySet());
// [Color4, Color3, Color1, Color2]


for (Map.Entry<String,String> entry : map.entrySet()) {
System.out.printf("%s -> %s%n", entry.getKey(), entry.getValue());
}
// Color4 -> White
// Color3 -> Green
// Color1 -> Red
// Color2 -> Blue

Note that the entries are iterated in arbitrary order. If you need a specific order, then you may consider e.g. LinkedHashMap

See also

Related questions

On iterating over entries:

On different Map characteristics:


On enum

You may want to consider using an enum and EnumMap instead of Map<String,String>.

See also

Related questions

this work for me:

HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");


Iterator iterator = meMap.keySet().iterator();
while( iterator. hasNext() )
{
Toast.makeText(getBaseContext(), meMap.get(iterator.next().toString()),
Toast.LENGTH_SHORT).show();
}

This with no warnings!

    HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");


for (Object o : meMap.keySet()) {
Toast.makeText(getBaseContext(), meMap.get(o.toString()),
Toast.LENGTH_SHORT).show();
}

Note: If you know Key, use this code

String value=meMap.get(key);
for (Object key : meMap.keySet()) {
String value=(String)meMap.get(key);
Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
 Iterator iterator = meMap.keySet().iterator();
while(iterator.hasNext() ){
//get key
String key=(String)iterator.next();
//toast value
Toast.makeText(getBaseContext(),""+(String)meMap.get(key),Toast.LENGTH_SHORT).show();


}