您可以使用每个ID链接到字符串列表一个HashMap:
Map<String, List<String>> dictionary = new HashMap<String,List<String>>();
现在让我们假设你读两个字符串:id
和word
。要将它们添加到字典中,您可以首先验证您的ID是否已被读取(使用方法containsKey()
) - 在这种情况下,您只需将该单词追加到与该ID相对应的列表中;或者,如果不是这种情况,您创建一个新的列表与这个词:
//If the list already exists...
if(dictionary.containsKey(id)) {
List<String> appended = dictionary.get(id);
appended.add(word); //We add a new word to our current list
dictionary.remove(id); //We update the map by first removing the old list
dictionary.put(id, appended); //and then appending the new one
} else {
//Otherwise we create a new list for that id
List<String> newList = new ArrayList<String>();
newList.add(word);
dictionary.put(id, newList);
}
然后,每当你想找回您的字符串列表一定的id,你可以简单地使用dictionary.get(ID);
你可以找到包含HashMap的Java documentation
你能提供一些你现有的代码吗? – rhgb