Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新HashMap中的ArrayList_Java - Fatal编程技术网

Java 更新HashMap中的ArrayList

Java 更新HashMap中的ArrayList,java,Java,我需要创建几个文档中单词的索引。 索引具有以下格式 字,{d1,f1,d2,f2…},值 word=多个文档中的多个单词 d1,d2,…=它出现在其中的文档的名称 f1,f2…=它出现在该文档中的次数 value=根据单词出现的文件数进行计算 到目前为止,我已经创建了两个类。IRS系统和参考计数 ReferenceCount具有文档ID(d1、d2..)和计数(f1、f2..) IRSystems具有ReferenceCount的arraylist和Hashmap[String,arraylis

我需要创建几个文档中单词的索引。 索引具有以下格式

字,{d1,f1,d2,f2…},值

word=多个文档中的多个单词

d1,d2,…=它出现在其中的文档的名称

f1,f2…=它出现在该文档中的次数

value=根据单词出现的文件数进行计算

到目前为止,我已经创建了两个类。IRS系统和参考计数

ReferenceCount具有文档ID(d1、d2..)和计数(f1、f2..)

IRSystems具有ReferenceCount的arraylist和Hashmap[String,arraylist(ReferenceCount)] 我一次读取一个文档中的所有单词,并将其命名为“标记” 我试图在HashMap中添加单词,如果单词已经存在于HashMap中,则查找该单词所属的文档,如果它来自同一文档,则更新计数。如果它来自不同的文档,则向arrayList添加新的documentId和新计数

到目前为止,我已经做到了这一点。 我有两个问题,若它来自同一个documnet,它不会增加字数。我无法实现“价值”

HashMap normalList=newhashmap();
而(st.hasMoreElements())
{
字符串标记=st.nextToken();
if(normalList.size()==0 | |!normalList.containsKey(标记))
{
rList=新的ArrayList();
rCount=新引用计数(名称);
rList.add(rCount);
normalList.put(令牌、rList);
}
else if(normalList.containsKey(令牌))
{
System.out.println(“找到匹配项”);
迭代器it=normalList.entrySet().Iterator();
while(it.hasNext())
{
Map.Entry对=(Map.Entry)it.next();
ArrayList rList1=新的ArrayList();
rList1=(ArrayList)pair.getValue();
用于(参考计数rC:rList1)
{
if(pair.getKey().equals(rC.getDocumentId()))
{
System.out.println(“找到匹配项2”);
rC.increment();
}
}
}
}
}
//显示hashmap的步骤
迭代器it=normalList.entrySet().Iterator();
while(it.hasNext())
{
Map.Entry对=(Map.Entry)it.next();
System.out.println(pair.getKey()+“,”);
ArrayList rList1=新的ArrayList();
rList1=(ArrayList)pair.getValue();
用于(参考计数rC:rList1)
{
rCount=新引用计数(名称);
System.out.println(rCount.getDocumentId()+”,“+rCount.getCount());
}
}
}

您使用了单词的地图。为什么不对文档ID使用相同的方法?您可以创建一个HashMaps的HashMap,如下所示:

HashMap<String, HashMap<String, Integer>> wordCountMap = 
       new HashMap<String, HashMap<String, Integer>>();
HashMap wordCountMap=
新的HashMap();
对于您的值,您可以创建一个单独的HashMap,其中单词作为键,计算值作为值:

HashMap<String, String> wordValueMap = new HashMap<String, String>(); 
HashMap wordValueMap=newhashmap();
对于每个单词,检查
wordCountMap.containsKey(newWord)
,如果不存在,则使用新的documentId和单词计数1创建内部HashMap。如果键存在,则获取现有的内部HashMap,然后检查文档ID是否存在,依此类推

最后,您可以在wordValueMap中分别维护计算出的

HashMap<String, String> wordValueMap = new HashMap<String, String>();