Android - Get value from HashMap
Asked Answered
F

10

87

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?

Falcate answered 6/8, 2010 at 9:10 Comment(5)
developer.android.com/reference/java/util/HashMap.htmlAlbuminuria
@Albuminuria ya i have already seen android-sdkFalcate
Maybe you should also read the available methods like keySet(), not just the descriptions.Superhuman
@Superhuman Dont know anything about HashMap....so by theory how can i come to know....btw thanx for help and supportFalcate
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.Superhuman
S
114
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();
}
Superhuman answered 6/8, 2010 at 9:12 Comment(4)
Note that iteration order is undefined. If you want the same order as the sets were added use LinkedHashMapPocked
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.Falcate
Just make a request to hashmap for the key and you will have it, I updated my code.Superhuman
how to add LinkedHashMap to return in same order, help please.Pertinacious
F
93

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

Fortitude answered 6/8, 2010 at 9:45 Comment(2)
import java.util.*; must be addedBambi
If you only want the values, this answer is clearly more understandableOlomouc
C
8

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();
    }
Capitalism answered 14/10, 2014 at 14:52 Comment(0)
A
7
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();
}
Anselmi answered 6/8, 2010 at 9:30 Comment(0)
S
4
for (Object key : meMap.keySet()) {
        String value=(String)meMap.get(key);
        Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
    }
Sorcery answered 5/9, 2017 at 10:16 Comment(0)
D
2
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();
}
Damien answered 6/8, 2010 at 9:26 Comment(0)
A
2

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();
}
Araxes answered 3/4, 2014 at 8:3 Comment(0)
H
1

Note: If you know Key, use this code

String value=meMap.get(key);
Hedonic answered 20/7, 2017 at 9:59 Comment(0)
S
1
 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();

                }
Subternatural answered 10/4, 2018 at 9:16 Comment(0)
S
0

Below code will generate unique random numbers list.

int size = 10; // Change according to your need
List<Integer> Lucky = new ArrayList<>();
while(Lucky.size()<size)
{
int r = new Random().nextInt(1000); 
if(!Lucky.contains(r)) Lucky.add(r); 
}
Speedwell answered 8/4 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.