繁体   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