簡體   English   中英

Java 中的 ConcurrentHashMap 類型未定義方法 split(String)

[英]Method split(String) is undefined for the type ConcurrentHashMap in Java

我正在嘗試編寫一個方法 AddWordsInCorpus,它將使用名為 lemmas 的 ConcurrentHashMap 中每個條目的字符串(獲取存儲在值中的字符串),按空格將它們拆分為單獨的單詞,然后將這些單詞添加到名為 ArrayList 的語料庫。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import helpers.JSONIOHelper;
    
public class DescriptiveStatistics {
            
        private static void StartCreatingStatistics(String filePath) {
            // create an object of the JSONIOHelper class
            JSONIOHelper JSONIO = new JSONIOHelper(); 
            JSONIO.LoadJSON(filePath); /
            ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure();
    
         private void AddWordsInCorpus(ConcurrentHashMap<String, String> lemmas) {
            
            //compile the  words in the corpus into a new ArrayList
            ArrayList<String> corpus = new ArrayList<String>(); 
            
            // a loop to get the values (words) and add them into corpus.
            for(Entry<String, String> entry : lemmas.entrySet()){
        
                String[] words = entry.getValue();      // error 1
                        for(String word : lemmas.split(" ")) {   // error 2
    
                            corpus.addAll(Arrays.asList(word));}
        }

我收到以下兩個錯誤:

錯誤 1. 類型不匹配:無法將 String 轉換為 String[]

錯誤 2. //ConcurrentHashMap<String,String> 類型的方法 split(String) 未定義

有人可以幫我嗎?

entry.getValue ()返回一個字符串而不是字符串數組:

String words = entry.getValue();      // error 1

並且可以拆分此字符串:

for(String word : words.split(" ")) {   // error 2

map ConcurrentHashMap<String, String> lemmas中的值是String而不是String[]類型,這就是您看到錯誤 1 的原因。

第二個錯誤是因為您試圖拆分 HashMap lemmas.split(" ") ,當然,您不能這樣做。

如果我理解正確,您的代碼應如下所示:

for (Entry<String, String> entry : lemmas.entrySet()) {
    String word = entry.getValue();     // error 1
    String[] words = word.split(" ");
    for (String w : words) {            // error 2
        corpus.addAll(w);
    } 
}

暫無
暫無

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

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