如何验证字符串是否已在哈希表中? java

how can I verify if an String is already in a hashtable? java

我有以下情况:我试图遍历 ArrayList 并将每个元素与 HashTable 进行比较。如果元素(字符串)相等,我想将其复制到新的 HashTable neu 中。直到这里一切顺利。 但我想,如果这个词已经在新的 HashTable neu 中,就不要再放它了,简单地在代表频率的值上加一个计数。 我想在这段代码中永远不会到达 else 块,因为 test 输出它从未显示过。 我知道在 contentEquals 之前它运作良好,在那里它 returns 我所有的词都是平等的。

那么:我如何验证字符串是否已经在 HashTable 中?我希望它不会重复,我已经看到很多类似的问题,但不知道如何在我的代码中正确使用它。

Hashtable<String, Long> neu = new Hashtable<String, Long>();
ArrayList<String> words = new ArrayList<String>();
Hashtable<String, Long> count = new Hashtable<String, Long>();

// adding input

for (String s : count.keySet()) { 

    for (String x : words) {
            if (x.contentEquals(s)) {  
                if (!neu.containsKey(s)) {  
                    neu.put(s, (long) 1);
                } else { 
                    long p = neu.get(s); 
                    p = p + 1;
                    neu.put(s, p);
                    System.out.println("test");
                }
            }
        }

输入如下: 计数:

        try {
        BufferedReader in = new BufferedReader(new FileReader(
                "griechenland_test.txt"));
        String str;

        while ((str = in.readLine()) != null) {
            str = str.toLowerCase(); // convert to lower case
            String[] words = str.split("\s+"); // split the line on
                                                // whitespace, would return
                                                // an array of words

            for (String word : words) {
                if (word.length() == 0) {
                    continue;
                }

                Long occurences = wordcount.get(word);

                if (occurences == null) {
                    occurences = (long) 1;
                } else {
                    occurences++;
                }

                count.put(word, occurences);
            }

        }

字数:

        BufferedReader br = new BufferedReader(new FileReader("outagain.txt")); 
for (String line = br.readLine(); line != null; line = br.readLine()) {
        String h[] = line.split("   ");

        words.add(h[0].toString());

    }

使用 containsKey() 方法检查给定键是否已在哈希表中。

static Hashtable<String, Long> map = new Hashtable<String, Long>();

    public static void main(String[] args)
    {
        map.put("test", 1l);

        System.out.println(map.containsKey("test")); // true
    }

对于字符串比较,使用 equals(Object) or equalsIgnoreCase(String) 方法。

所以你的代码应该是这样的...

for (String s : count.keySet())
        {
            for (String x : words)
            {
                if (x.equals(s))
                    neu.put(s, neu.containsKey(s) ? neu.get(s) + 1 : 1l);
            }
        }

请验证您的 wordscount 的内容。这应该是问题所在,因为您的代码运行良好。

我能做到!我认为故障实际上是​​输入的方式,那里没有重复,因此从未达到 "else" !我只是消除了那个条件并稍微改变了结构。 无论如何,感谢您的努力! :)

for (String x : hash.keySet()) {
        long neu = hash.get(x);
        for (String s : words) {
            if (x.equals(s)) {
                neuS.add(x);
                neuZ.add(neu);
                disc = disc + 1;
            }
        }
    }