中添加新元素
我有一个问题要在此hashmap中存储数据,我正在java中编程。
我的系统由一些聊天组成,在哈希地图中,我必须将聊天插入索引和连接到特定聊天的用户列表,我的问题是hashmap的初始化,因为我只需要输入聊天,但是阵列列表是空的,因为没有连接用户,只有我不理解如何正确执行此操作。
这是我的代码的一些示例:
public class Master {
private HashMap<String, ArrayList<String>> chatBox;
public Master() {
chatBox = new HashMap<String, ArrayList<String>>();
}
public insert() {
FileReader fr;
BufferedReader br;
try {
fr = new FileReader("listChat.txt");
br = new BufferedReader(fr);
while(true) {
String topic = br.readLine();
if(topic == null)
break;
chatBox.put(topic, null);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我建议您通过在hashmap中添加新元素时创建一个空数列来通过创建代码来更改代码:
while(true) {
String topic = br.readLine();
if(topic == null)
break;
chatBox.put(topic, new ArrayList<String>());
}
当您必须使用消息更新此主题时,您将获得关键"主题"的值,然后在ArrayList