簡體   English   中英

如何驗證哈希表中是否已存在字符串? 爪哇

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

我有以下情況:我試圖遍歷ArrayList並將每個元素與HashTable進行比較。 如果元素(字符串)相等,我想將其復制到新的HashTable neu中 到這里為止一切順利。 但是我想,如果該單詞已經在新的HashTable neu中 ,則不要再放它,只需簡單地在代表頻率的值中添加一個計數即可。 我想這段代碼中永遠不會到達else-block,因為它永遠不會顯示測試輸出。 我知道在contentEquals運作良好之前,它會向我返回所有相等的單詞。

因此:如何驗證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)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);
            }
        }

請核實您的words內容並count 這應該是問題所在,因為您的代碼運行正常。

我可以解決! 我以為錯誤實際上是從輸入開始的方式,那里沒有重復,因此永遠無法達到“其他”! 我只是消除了這種情況,並改變了結構。 無論如何,謝謝您的努力! :)

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;
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM