字符串ArrayList的ArrayList

时间:2013-10-18 16:56:04

标签: java arraylist

如何跟踪String元素的ArrayList的ArrayList中的元素并从中创建HashMap?我需要知道如何访问每个内部ArrayList中的字符串元素,如果它的唯一添加它作为HashMap中的键并将其计数设置为1,则任何内部ArrayList中任何后续存在的特定字符串都会增加计数为那个字符串。

1 个答案:

答案 0 :(得分:2)

因为我心情很好:

List<List<String>> outer = getOuter();
Map<String, Integer> wordCount = new HashMap<>();
for(List<String> inner : outer) {
  for(String word : inner) {
    if(!wordCount.contains(word)) {
      wordCount.put(word,1);
    } else {
      wordCount.put(word,wordCount.get(word)+1);
    }
  }
}