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

I am very new to Android development and I am trying to use HashMap in an Android project. Now, I am making a sample project for learning android. I just store the keys and values ​​in the HashMap, I want to show the keys and their values ​​in the EditView. In my sample project, I used the code below. But, the first key and value is only to print in the 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 , only printing is performed. I want to print all the keys and their value in an EditText. Can someone please tell me where I am doing wrong? Thank you in advance.

+58
java android hashmap key
Jan 18 '12 at 12:05
source share
11 answers

Firstly, there are errors in your code, i.e. you are missing a semicolon and closing parenthesis in a for loop.

Then, if you are trying to add values ​​to the view, you should use textview.appendText (), not .setText ().

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

+7
Jan 18 2018-12-12T00:
source share
 for (Map.Entry<String,String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // do stuff } 
+163
Jan 18 '12 at 12:10
source share

This is because your TextView receives new text at each iteration and the discarded value prevails. Concatenate StringBuilder strings and set the TextView value after the loop. You can also use this type of loop:

 for (Map.Entry<String, String> e : map.entrySet()) { //to get key e.getKey(); //and to get value e.getValue(); } 
+12
Jan 18 '12 at 12:16
source share
 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)); } 
+12
May 28 '14 at 20:34
source share

Java 8

 map.entrySet().forEach(System.out::println); 
+4
Apr 24 '17 at 13:52 on
source share

This code has been verified and works.

 public void dumpMe(Map m) { dumpMe(m, ""); } private void dumpMe(Map m, String padding) { Set s = m.keySet(); java.util.Iterator ir = s.iterator(); while (ir.hasNext()) { String key = (String) ir.next(); Object value = m.get(key); if (value == null) continue; if (value instanceof Map) { System.out.println (padding + key + " = {"); dumpMe((Map)value, padding + " "); System.out.println(padding + "}"); } else if (value instanceof String || value instanceof Integer || value instanceof Double || value instanceof Float || value instanceof Long ) { System.out.println(padding + key + " = " + value.toString()); } else { System.out.println(padding + key + " = UNKNOWN OBJECT: " + value.toString()); // You could also throw an exception here } } // while } // dumpme 

Charles.

+2
Jun 14 '12 at 21:16
source share

With Java 8:

 map.keySet().forEach(key -> System.out.println(key + "->" + result.get(key))); 
+2
Nov 02 '16 at 22:51
source share

You can make it easier with Gson:

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

The result will look like this:

 I/YOURTAG: SomeText: {"key1":"value1","key2":"value2"} 
+2
May 08 '17 at 8:50
source share
 String text=""; for (Iterator i = keys.iterator(); i.hasNext() { String key = (String) i.next(); String value = (String) map.get(key); text+=key + " = " + value; } textview.setText(text); 
+1
Jan 18 '12 at 12:10
source share

you can use this code:

 for (Object variableName: mapName.keySet()){ variableKey += variableName + "\n"; variableValue += mapName.get(variableName) + "\n"; } System.out.println(variableKey + variableValue); 

this code ensures that all keys will be stored in a variable and then printed!

+1
May 11 '14 at 4:55
source share
 public void dumpMe(Map m) { dumpMe(m, ""); } private void dumpMe(Map m, String padding) { Set s = m.keySet(); java.util.Iterator ir = s.iterator(); while (ir.hasNext()) { String key = (String) ir.next(); AttributeValue value = (AttributeValue)m.get(key); if (value == null) continue; if (value.getM() != null) { System.out.println (padding + key + " = {"); dumpMe((Map)value, padding + " "); System.out.println(padding + "}"); } else if (value.getS() != null || value.getN() != null ) { System.out.println(padding + key + " = " + value.toString()); } else { System.out.println(padding + key + " = UNKNOWN OBJECT: " + value.toString()); // You could also throw an exception here } } // while } //This code worked for me. 
+1
03 Sep '16 at 5:29
source share



All Articles