提问人:Bluetail 提问时间:8/8/2022 最后编辑:JensBluetail 更新时间:8/8/2022 访问量:259
在 Java 中,未为 ConcurrentHashMap 类型定义 split(String)
Method split(String) is undefined for the type ConcurrentHashMap in Java
问:
我正在尝试编写一个方法AddWordsInCorpus,它将使用ConcurrentHashMap中每个条目的字符串,称为lemmas(获取存储在值中的字符串),通过空格将它们拆分为单独的单词,然后将这些单词添加到名为corpus的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) 方法>
谁能帮我解决这个问题?
答:
0赞
Jens
8/8/2022
#1
entry.getValue ()
返回一个字符串而不是一个字符串数组:
String words = entry.getValue(); // error 1
这个字符串可以拆分:
for(String word : words.split(" ")) { // error 2
0赞
Nemanja
8/8/2022
#2
地图中的值类型不是,这就是您看到错误 1 的原因。ConcurrentHashMap<String, String> lemmas
String
String[]
第二个错误是因为你试图拆分你的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);
}
}
评论