如何将我的所有数据从哈希图打印到我的文本区域



如何将我的所有数据从哈希图打印到我的文本区域

public static void main(String[] args){
    HashMap<String, String> data = new HashMap<String, String>();
    data.put("nama", "Yudi Setiawan");     
    data.put("kelas", "TI A MALAM");     
    data.put("hobi", "Programming");

'我希望将所有键和值附加到我的文本区域中

你可以尝试类似的东西

for (String key : data.keySet()){
    System.out.println(key + data[key]);
}

你应该遍历你的 HashMap:

public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }
}

遍历映射,使用 StringBuilder 构建包含所有键和值的字符串,然后将结果存储在JTextArea中。

JTextArea textArea = new JTextArea();
StringBuilder str = new StringBuilder();
for (String key : data.keySet()) {
    str.append(key)
       .append("=")
       .append(data.get(key))
       .append("n");
}
textArea.setText(str.toString);

最新更新