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

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.


Solution 1:

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

Solution 2:

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 3:

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"}