【问题标题】:Detecting multiple values in a list检测列表中的多个值
【发布时间】:2013-10-12 05:12:34
【问题描述】:

我遇到了这个问题:我要编写一个方法 contains3,它接受一个字符串列表作为参数,如果任何单个字符串在列表中至少出现 3 次,则返回 true,否则返回 false。我需要使用地图。

当一个词出现三个实例时,仍然不返回true;我无法确定哪里出了问题。

这是我所拥有的:

private static boolean contains3(List<String> thing) {
    Map<String, Integer> wordCount = new TreeMap<String, Integer>();
    for (String s: thing) {
        String word = s;

        if (wordCount.containsKey(word)) { // seen before.
            int count = wordCount.get(word);
            wordCount.put(word, count + 1);
        } else {
            wordCount.put(word, 1); // never seen before.
        }

        if (wordCount.containsValue(3)) {
            return true;
        } else {
            return false;
        }

    }
    return false;
}

【问题讨论】:

  • 当一个词出现三个实例时,仍然不返回true;我无法确定哪里出了问题。
  • @JackL。你不能使用集合中的本地方法吗?
  • 不确定,但是我应该使用地图来解决问题。
  • 调试会有所帮助
  • 是的......但由于某种原因,我的 Eclipse 在我下载深色 UI 后调试时不显示变量。可能是一个错误;我肯定需要修复我的 Eclipse。

标签: java list map


【解决方案1】:

问题出在这里:

if (wordCount.containsValue(3)) {
    //...

您应该使用键获取值,换句话说,您正在计数的word

if (wordCount.get(word) >= 3) {
    return true;
}

请注意,我从 if 语句中删除了 return false;,因为它会在第一次迭代中破坏方法。


作为建议,您可以使用HashMap 而不是TreeMap增强您的方法的性能,因为putgetHashMap 中的时间为 O( 1) (恒定时间) 而TreeMap 的时间为 O(log n)。

【讨论】:

  • 另外,HashMap 是正确的选择,而不是 TreeMap(效率更高,因为不需要排序)。
【解决方案2】:

尝试使用以下代码。

private static boolean contains3(List<String> thing) {
    Map<String, Integer> wordCount = new TreeMap<String, Integer>();
        thing.add("hi");
        thing.add("hi");
        thing.add("hi");
        thing.add("hia");
        thing.add("hi3");
        for (String s: thing) {
            String word = s;

            if (wordCount.containsKey(word)) { // seen before.
                int count = wordCount.get(word);
                wordCount.put(word, count + 1);
            } else {
                wordCount.put(word, 1); // never seen before.
            }
        }
            if (wordCount.containsValue(3)) {
                return true;
            } else {
            return false;}

【讨论】:

    【解决方案3】:

    您在添加每个单词时运行此代码:

            if (wordCount.containsValue(3)) {
                return true;
            } else {
                return false;
    

    添加第一个单词时测试将失败,您将立即返回false。将该块移到方法的末尾,在您当前拥有return false 的最后一行中,以便仅在计算完所有单词后进行检查。

    【讨论】:

      【解决方案4】:

      if (wordCount.containsValue(3)) {
              return true;
          } else {
              return false;
          }
      

      在for循环之外

      【讨论】:

        【解决方案5】:

        在初始 if 块中检查 count 是否 >= 3 效率更高

           if (wordCount.containsKey(word)) { // seen before.
                int count = wordCount.get(word) + 1;
                if(count >= 3) {
                      return true;
                }
                wordCount.put(word, count);
            }
        

        并删除以下 if else 块

            if (wordCount.containsValue(3)) {
                return true;
            } else {
                return false;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-09
          • 2020-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多