[英]How to print all key and values from HashMap in Android?
我对Android开发很陌生,我正在尝试在Android示例项目中使用HashMap。 现在,我正在做学习android的示例项目。 我只是在 HashMap 中存储键和值,我想在 EditView 中显示键和它们的值。 我在我的示例项目中遵循了以下代码。 但是,第一个键和值仅在 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);
}
在 EditView iOS = 100
中仅打印。 我想在 EditText 中打印所有键及其值。 谁能告诉我我哪里做错了? 提前致谢。
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// do stuff
}
这是因为您的 TextView 在每次迭代时都会收到新文本,并且丢弃了先前的值。 通过 StringBuilder 连接字符串并在循环后设置 TextView 值。 您也可以使用这种类型的循环:
for (Map.Entry<String, String> e : map.entrySet()) {
//to get key
e.getKey();
//and to get value
e.getValue();
}
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));
}
您可以使用 Gson 更轻松地做到这一点:
Log.i(TAG, "SomeText: " + new Gson().toJson(yourMap));
结果将如下所示:
I/YOURTAG: SomeText: {"key1":"value1","key2":"value2"}
首先,您的代码中有错误,即。 您在 for 循环中缺少分号和右括号。
然后,如果您尝试将值附加到视图,您应该使用 textview.appendText(),而不是 .setText()。
这里有一个类似的问题: how to change text in Android TextView
使用 Java 8:
map.keySet().forEach(key -> System.out.println(key + "->" + map.get(key)));
爪哇 8
map.entrySet().forEach(System.out::println);
for (String entry : map.keySet()) {
String value = map.get(entry);
System.out.print(entry + "" + value + " ");
// do stuff
}
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);
这段代码已经过测试并且可以正常工作。
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
查尔斯。
您可以使用此代码:
for (Object variableName: mapName.keySet()){
variableKey += variableName + "\n";
variableValue += mapName.get(variableName) + "\n";
}
System.out.println(variableKey + variableValue);
这段代码将确保所有的键都存储在一个变量中,然后打印出来!
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.
打印所有密钥:
myMap.keys().toList().joinToString()
要打印所有条目:
myMap.map { "${it.key} :: ${it.value}" }.toList().joinToString()
对于每个点击这个来找出你的 HashMap 的内容的人来说, toString
方法( docs )实际上适用于大多数对象。 (注意:java 数组不是对象!)
所以这对于调试目的来说非常好:
System.out.println(myMap.toString());
>>> {key1=value1, key2=value2}
科特林答案
for ((key, value) in map.entries) {
// do something with `key`
// so something with `value`
}
您可能会找到包含 filterValues 的其他解决方案。 请记住,使用 filterValues 检索 Key 值将包括大括号 []。
val key = map.filterValues {it = position}.keys
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.