Android - Get value from HashMap
Solution 1
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();
}
Solution 2
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
- Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces
-
Java Tutorials/Collections - The
Map
interface - Java Language Guide/The for-each loop
Related questions
On iterating over entries:
- Iterate Over Map
-
iterating over and removing from a map
- If you want to modify the map while iterating, you'd need to use its
Iterator
.
- If you want to modify the map while iterating, you'd need to use its
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
Solution 3
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();
}
Solution 4
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();
}
Solution 5
for (Object key : meMap.keySet()) {
String value=(String)meMap.get(key);
Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
Related videos on Youtube
![Paresh Mayani](https://i.stack.imgur.com/9sb69.png?s=256&g=1)
Paresh Mayani
Mobile Development & Project Management/Delivery Consultant | Co-Founder | Start-up Consultant | Community Organiser @ GDG Ahmedabad | Certified Scrum Master Exploring the horizon of the software industry since 11+ years. Let's connect over twitter: @pareshmayani OR LinkedIn What I do: Co-Founder / CEO @ SolGuruz Founder/Organiser, Google Developers Group, Ahmedabad Blog @ TechnoTalkative Achievements: 11th person to earn Android gold badge 4th person to earn android-layout bronze badge. 20th person in the list of Highest reputation holder from India
Updated on July 08, 2022Comments
-
Paresh Mayani almost 2 years
I have tried to search on HashMap in Android, but getting problem:
Consider this example:
HashMap<String, String> meMap=new HashMap<String, String>(); meMap.put("Color1","Red"); meMap.put("Color2","Blue"); meMap.put("Color3","Green"); meMap.put("Color4","White");
now I want to iterate it and get the value of each color and want to display in "Toast". how do I display it?
-
clamp almost 14 years
-
Paresh Mayani almost 14 years@clamp ya i have already seen android-sdk
-
Pentium10 almost 14 yearsMaybe you should also read the available methods like
keySet()
, not just the descriptions. -
Paresh Mayani almost 14 years@Pentium10 Dont know anything about HashMap....so by theory how can i come to know....btw thanx for help and support
-
Pentium10 almost 14 yearsThe SDK contains descriptions about the methods it has, you should check and see what kind of methods can be run on HashMap prior to asking.
-
-
Key almost 14 yearsNote that iteration order is undefined. If you want the same order as the sets were added use LinkedHashMap
-
Paresh Mayani almost 14 yearsthe above code is iterated over only on "key"..but not for "Value"..such as it displays only "color1", "color2"...etc. instead of "red", "blue", etc.
-
Pentium10 almost 14 yearsJust make a request to hashmap for the key and you will have it, I updated my code.
-
Durai Amuthan.H about 10 yearsimport java.util.*; must be added
-
Jorny almost 7 yearshow to add LinkedHashMap to return in same order, help please.
-
E.Akio over 4 yearsIf you only want the values, this answer is clearly more understandable