Android - Get value from HashMap

194,441

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

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

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();
    }
Share:
194,441

Related videos on Youtube

Paresh Mayani
Author by

Paresh Mayani

Mobile Development &amp; 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, 2022

Comments

  • Paresh Mayani
    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
      clamp almost 14 years
    • Paresh Mayani
      Paresh Mayani almost 14 years
      @clamp ya i have already seen android-sdk
    • Pentium10
      Pentium10 almost 14 years
      Maybe you should also read the available methods like keySet(), not just the descriptions.
    • Paresh Mayani
      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
      Pentium10 almost 14 years
      The 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
    Key almost 14 years
    Note that iteration order is undefined. If you want the same order as the sets were added use LinkedHashMap
  • Paresh Mayani
    Paresh Mayani almost 14 years
    the 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
    Pentium10 almost 14 years
    Just make a request to hashmap for the key and you will have it, I updated my code.
  • Durai Amuthan.H
    Durai Amuthan.H about 10 years
    import java.util.*; must be added
  • Jorny
    Jorny almost 7 years
    how to add LinkedHashMap to return in same order, help please.
  • E.Akio
    E.Akio over 4 years
    If you only want the values, this answer is clearly more understandable