Java 包含方法和计数器更新

Java 包含方法和计数器更新,java,nodes,counter,trie,Java,Nodes,Counter,Trie,因此,我正在完成我的程序,测试将通过测试程序中的一个单词列表,并使用前缀,我对其进行筛选,只返回匹配的单词 下面是我的contains方法的代码和测试方法的片段,我只是想知道我做错了什么 //包含方法 public boolean containsKey(TrieMapNode current, String curKey) { // recursively get the value for the current node String value = get(current

因此,我正在完成我的程序,测试将通过测试程序中的一个单词列表,并使用前缀,我对其进行筛选,只返回匹配的单词

下面是我的contains方法的代码和测试方法的片段,我只是想知道我做错了什么

//包含方法

public boolean containsKey(TrieMapNode current, String curKey) {
    // recursively get the value for the current node
    String value = get(current,curKey);
    // if value if null or empty, key is not found return false
    if(value == null) {
        return false;
    }else if (value.equals("")) {
        return false;
    } else {
        return true;
    }
}
//测试程序

    import java.util.HashSet;
    import java.util.ArrayList;

public class TriMapTester{
    public static void main(String[] args){
    HashSet<String> posWords = getPositiveWords();
    HashSet<String> negWords = getNegativeWords();
    TrieMapInterface map = new TrieMap();

    //Add all of the positive words
    for(String word: posWords){
      map.put(word, word);
    }

    int countErrors = 0;
    int totalErrors = 0;

    //Check that the map contains all positive words
    for(String s: posWords){
      if(!map.containsKey(s)){
        countErrors++;
      }
    }
    System.out.println("Number of missed keys after searching all positive words: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check that the correct value is associated with each key
    for(String s: posWords){
      if(!map.containsKey(s) || !map.get(s).equals(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrect values for keys: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check negative words. Make sure they do not show up in map if they are not also in positive words.
    for(String s: negWords){
      if(map.containsKey(s) && !posWords.contains(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrectly found keys after searching all negative words: " + countErrors);
    totalErrors += countErrors;

    //Check the values returned for a number of prefix values
    //Compares the returned result to the true result
    String[] searchPrefixes = {"add", "bril", "cat", "cri", "derri", "mar", "tra", "lor", "marveled", "rit"};
    for(String prefix: searchPrefixes){
      System.out.println("Getting values for prefix: " + prefix);
      ArrayList<String> realResult = matchPrefix(prefix, posWords);
      ArrayList<String> yourResult = map.getValuesForPrefix(prefix);
      System.out.println("Result should contain: " + realResult);
      System.out.println("    Your map returned: " + yourResult);
      countErrors = countDifference(realResult, yourResult);
      totalErrors += countErrors;
      System.out.println("Error Count: " + countErrors);
    }
    System.out.println("Total Errors: " + totalErrors);
  }

  public static int countDifference(ArrayList<String> realResult, ArrayList<String> yourResult){
    int result = 0;
    for(String s: realResult){
      if(!yourResult.contains(s)){
        result++;
      }
    }

    for(Object s: yourResult){
      if(!realResult.contains((String)s)){
        result++;
      }
    }
    return result;
  }

  public static ArrayList<String> matchPrefix(String prefix, HashSet<String> words){
   ArrayList<String> result = new ArrayList<String>();

    for(String s: words){
      if(s.startsWith(prefix)){
        result.add(s);
      }
    }
   return result;
  }

  public static HashSet<String> getPositiveWords(){
HashSet<String> result = new HashSet<String>();
String[] pos = new String[]{"abound", "abounds", "abundance", "abundant", "accessable", "accessible", "acclaim", "acclaimed", "acclamation"}; 
for(String s: pos){
  result.add(s);
}
return result;
 }

  public static HashSet<String> getNegativeWords(){
HashSet<String> result = new HashSet<String>();
String[] neg = new String[]{"abnormal", "abolish", "abominable", "abominably", "abominate", "abomination", "abort", "aborted", "aborts", "abrade", "abrasive", "abrupt", "abruptly", "abscond", "absence", "absent-minded"}; 
for(String s: neg){
  result.add(s);
}
return result;
  }
import java.util.HashSet;
导入java.util.ArrayList;
公共类TriMapTester{
公共静态void main(字符串[]args){
HashSet posWords=getPositiveWords();
HashSet negWords=getNegativeWords();
TriemaInterface map=新建TrieMap();
//加上所有积极的词
for(字符串字:posWords){
地图。放(字,字);
}
int计数器错误=0;
int totalErrors=0;
//检查地图是否包含所有肯定词
for(字符串s:posWords){
如果(!map.containsKey){
计数器++;
}
}
System.out.println(“搜索所有肯定词后丢失的键数:“+counterrs”);
总误差+=反误差;
反错误=0;
//检查每个键是否关联了正确的值
for(字符串s:posWords){
如果(!map.containsKey(s)| |!map.get(s).equals(s)){
计数器++;
}
}
System.out.println(“键的错误值数:“+计数器错误”);
总误差+=反误差;
反错误=0;
//勾选否定词。如果否定词不在肯定词中,确保它们不会出现在地图上。
for(字符串s:negWords){
if(map.containsKey和&!posWords.contains){
计数器++;
}
}
System.out.println(“搜索所有否定词后错误找到的键数:“+counterrs”);
总误差+=反误差;
//检查返回的值以获取多个前缀值
//将返回的结果与真实结果进行比较
String[]searchPrefixes={“add”、“bril”、“cat”、“cri”、“derri”、“mar”、“tra”、“lor”、“惊奇”、“rit”};
for(字符串前缀:searchPrefixes){
System.out.println(“获取前缀:“+前缀”的值);
ArrayList realResult=matchPrefix(前缀,posWords);
ArrayList yourResult=map.getValuesForPrefix(前缀);
System.out.println(“结果应包含:“+realResult”);
System.out.println(“您的地图返回:“+yourResult”);
计数器错误=计数差异(realResult,yourResult);
总误差+=反误差;
System.out.println(“错误计数:+计数器错误”);
}
System.out.println(“总错误:+totalErrors”);
}
公共静态int countDifference(ArrayList realResult,ArrayList yourResult){
int结果=0;
for(字符串s:realResult){
如果(!yourResult.contains)){
结果++;
}
}
对于(对象s:您的结果){
如果(!realResult.contains((字符串)s)){
结果++;
}
}
返回结果;
}
公共静态ArrayList matchPrefix(字符串前缀、哈希集单词){
ArrayList结果=新建ArrayList();
for(字符串s:单词){
如果(s.startsWith(前缀)){
结果。添加(s);
}
}
返回结果;
}
公共静态哈希集getPositiveWords(){
HashSet result=新的HashSet();
String[]pos=新字符串[]{“丰富”、“丰富”、“丰富”、“可访问”、“可访问”、“喝彩”、“受欢迎”、“喝彩”};
用于(字符串s:pos){
结果。添加(s);
}
返回结果;
}
公共静态哈希集getNegativeWords(){
HashSet result=新的HashSet();
字符串[]neg=新字符串[]{“异常”、“废除”、“可恶”、“可恶”、“可恶”、“可恶”、“可恶”、“中止”、“中止”、“中止”、“磨损”、“磨损”、“突然”、“突然”、“潜逃”、“缺席”、“心不在焉”};
for(字符串s:neg){
结果。添加(s);
}
返回结果;
}

现在,我在运行测试时得到的输出是正确的包含单词,但是错误的数量仍然是0,尽管使用了contains方法,所以我不确定我做错了什么。如果有任何帮助,将不胜感激

if(map.containsKey(s) && !posWords.contains(s))
   countErrors++;
map&posWords包含相同的数据,您可以检查它是否为真,也可以检查它是否为假,这将永远不会执行
countErrors++
。这就是为什么countErrors为0