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.
Print HashMap Elements in Java This is the simplest way to print HashMap in Java. Just pass the reference of HashMap into the println() method, and it will print key-value pairs into the curly braces.
This is the most basic and easiest method to print out HashMap in java. Pass the HashMap reference to the System. out. println, and the HashMap will output the key-value of the elements enclosed in curly brackets.
Using toString() For displaying all keys or values present on the map, we can simply print the string representation of keySet() and values() , respectively. That's all about printing out all keys and values from a Map in Java.
for (Map.Entry<String,String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // do stuff }
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With