How to print all key and values from HashMap in Android?

247,519

Solution 1

First, there are errors in your code, ie. you are missing a semicolon and a closing parenthesis in the for loop.

Then, if you are trying to append values to the view, you should use textview.appendText(), instead of .setText().

There's a similar question here: how to change text in Android TextView

Solution 2

for (Map.Entry<String,String> entry : map.entrySet()) {
  String key = entry.getKey();
  String value = entry.getValue();
  // do stuff
}

Solution 3

It's because your TextView recieve new text on every iteration and previuos value thrown away. Concatenate strings by StringBuilder and set TextView value after loop. Also you can use this type of loop:

for (Map.Entry<String, String> e : map.entrySet()) {
    //to get key
    e.getKey();
    //and to get value
    e.getValue();
}

Solution 4

You can do it easier with Gson:

Log.i(TAG, "SomeText: " + new Gson().toJson(yourMap));

The result will look like:

I/YOURTAG: SomeText: {"key1":"value1","key2":"value2"}

Solution 5

HashMap <Integer,Integer> hm = new HashMap<Integer,Integer>();

Set<Integer> keys = hm.keySet();  //get all keys
for(Integer i: keys)
{
    System.out.println(hm.get(i));
}
Share:
247,519
Gopinath
Author by

Gopinath

Updated on July 05, 2022

Comments

  • Gopinath
    Gopinath almost 2 years

    I am very new for Android development, and I am trying to use HashMap in Android sample project. Now, am doing sample project for learn android. I just store keys and values in HashMap, i want to show the keys and their values in EditView. I followed below code in my sample project. But, first key and value only printing in EditView.

       Map<String, String> map = new HashMap<String,String>();
       map.put("iOS", "100");
       map.put("Android", "101");
       map.put("Java", "102");
       map.put(".Net", "103");
    
       Set keys = map.keySet();
    
       for (Iterator i = keys.iterator(); i.hasNext(); ) {
           String key = (String) i.next();
           String value = (String) map.get(key);
           textview.setText(key + " = " + value);
       }
    

    In EditView iOS = 100 is only printing. I want to print all keys and their value in EditText. Can anyone please tell me where i am doing wrong? Thanks in advance.

  • Dean Povey
    Dean Povey over 12 years
    This is the best answer, but see also @Shadow for how to use Java 5 for and Map.Entry properly.
  • moberme
    moberme about 10 years
    Thank you, added this to my utils
  • JimHawkins
    JimHawkins almost 8 years
    Please look at How to Answer
  • Nathan Tuggy
    Nathan Tuggy over 7 years
    Could you please edit in an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.
  • mdev
    mdev over 7 years
    OP wants to print all keys and value pairs. I dont think any explanation is needed here. It is an very old thread, already answered, and also no brain thing. I have already written if someone is looking for a solution in Java8, he/she can do something like that.
  • Tihamer
    Tihamer about 4 years
    mdev - There are many newbies here who would benefit from an explanation, even if it is obvious to an expert like you. If you would have provided an explanation, you might have discovered that your solution is incorrect; (teh "result" variable is unspecified). It should be: map.keySet().forEach(key -> System.out.println(key + "->" + map.get(key))); Where the -> separates the parameters (left-side) from the implementation (right side). See howtodoinjava.com/java8/foreach-method-example and docs.oracle.com/javase/tutorial/java/javaOO/…
  • mdev
    mdev about 4 years
    Tihamer Thanks for pointing the typo. I have edited the answer.
  • user207421
    user207421 about 4 years
    Doesn't address the actual problem with the OP's code, which is almost identical.
  • user207421
    user207421 about 4 years
    The OP wants to display all the keys and values in a text field, and his only problem is in that part of it.
  • Pooja
    Pooja almost 4 years
    Helpful. Thanks a lot.