Java 检测列表中的多个值

Java 检测列表中的多个值,java,list,map,Java,List,Map,我遇到了这个问题:我要编写一个包含3的方法,该方法接受字符串列表作为参数,如果列表中有任何字符串出现至少3次,则返回true,否则返回false。我需要一张地图 当一个单词有三个实例时,它仍然不返回true;我很难找到哪里出了问题 以下是我所拥有的: private static boolean contains3(List<String> thing) { Map<String, Integer> wordCount = new TreeMap<Strin

我遇到了这个问题:我要编写一个包含3的方法,该方法接受字符串列表作为参数,如果列表中有任何字符串出现至少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;
}
private静态布尔包含3(列表内容){
Map wordCount=new TreeMap();
用于(字符串s:对象){
字符串字=s;
if(wordCount.containsKey(word)){//以前见过。
int count=wordCount.get(word);
wordCount.put(word,count+1);
}否则{
wordCount.put(word,1);//以前从未见过。
}
if(wordCount.containsValue(3)){
返回true;
}否则{
返回false;
}
}
返回false;
}
问题在于:

if (wordCount.containsValue(3)) {
    //...
您应该使用键获取值,换句话说,就是您正在计数的
单词

if (wordCount.get(word) >= 3) {
    return true;
}
注意,我删除了
返回false
来自此
if
语句,因为它将在第一次迭代中中断该方法



作为建议,您可以使用a而不是
TreeMap
来增强方法的性能,因为
HashMap
中的
put
get
时间是O(1)(常量时间),而's是O(logn)。

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

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

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

尝试使用以下代码

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;}
private静态布尔包含3(列表内容){
Map wordCount=new TreeMap();
事物。添加(“嗨”);
事物。添加(“嗨”);
事物。添加(“嗨”);
添加(“hia”);
添加(“hi3”);
用于(字符串s:对象){
字符串字=s;
if(wordCount.containsKey(word)){//以前见过。
int count=wordCount.get(word);
wordCount.put(word,count+1);
}否则{
wordCount.put(word,1);//以前从未见过。
}
}
if(wordCount.containsValue(3)){
返回true;
}否则{
返回false;}
put


在for循环之外

在初始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;
    }

当一个单词有三个实例时,它仍然不会返回true;我很难找到哪里出了问题。@JackL。你不能从集合中使用本机方法吗?不确定,但我应该使用映射来分类。调试会有帮助的是的……但由于某些原因,我的Eclipse没有显示变量在我下载我的黑暗UI后进行调试时启用。可能是一个bug;我肯定需要修复我的Eclipse。此外,这里正确的选择是
HashMap
,而不是
TreeMap
(因为不需要排序,所以效率更高)。
    if (wordCount.containsValue(3)) {
        return true;
    } else {
        return false;
    }