Hashmap get函数返回null [英] Hashmap get function returns null
问题描述
我有一个hashmap,它是
public HashMap< String,ArrayList< Integer>> invertedList;
invertedList.toString():{ryerson = [0,2,3],23 = [3],award = [1],andisheh = [0,2 ]}
在我输入的同一个观察列表中:
invertedList.get(ryerson)
我得到null作为结果,也在代码中。正如你所看到的,ryerson已经在我的invertedList中作为关键字了,我应该得到[0,2,3] !!!这里发生了什么?我很困惑!
我知道ArrayList存在一个值问题,因为我测试了Integer作为值,它工作正常,但仍然不知道如何解决它。我是java的新手,曾经与C#一起工作。
invertedList的完整代码:
public class InvertedIndex {
public HashMap< String,ArrayList< Integer>> invertedList;
public ArrayList< String>文件;
public InvertedIndex(){
invertedList = new HashMap< String,ArrayList< Integer>>();
documents = new ArrayList< String>();
}
public void buildFromTextFile(String fileName)throws IOException {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int documentId = 0;
while(true){
String line = bufferedReader.readLine();
if(line == null){
break;
}
String [] words = line.split(\\W +);
for(String word:words){
word = word.toLowerCase();
if(!invertedList.containsKey(word))
invertedList.put(word,new ArrayList< Integer>());
invertedList.get(word).add(documentId);
}
documents.add(line);
documentId ++;
}
bufferedReader.close();
}
测试代码:
@Test
public void testBuildFromTextFile()throws IOException {
InvertedIndex invertedIndex = new InvertedIndex();
invertedIndex.buildFromTextFile(input.tsv);
Assert.assertEquals({ryerson = [0,2,3],23 = [3],award = [1],andisheh = [0,2]},invertedIndex.invertedList.toString()) ;
ArrayList<整数> resultIds = invertedList.get(ryerson);
ArrayList<整数> expectedResult = new ArrayList< Integer>();
expectedResult.add(0);
expectedResult.add(2);
expectedResult.add(3);
Assert.assertEquals(expectedResult,resultIds);
}
第一个Assert工作正常,第二个,resultIds为null。如果我正在阅读这个权利,并假设正确,这个测试函数是在InvertedIndex类中。我只是做了这个假设,因为这一行
ArrayList< Integer> resultIds = invertedList.get(ryerson);
实际上应该是不可编译的,因为没有称为invertedList的局部变量。
该行应为
ArrayList< Integer> resultIds = invertedIndex.invertedList.get(ryerson);
I have a hashmap which is
public HashMap<String, ArrayList<Integer>> invertedList;
I show you my invertedList in watch list during debugging:
invertedList.toString(): "{ryerson=[0, 2, 3], 23=[3], award=[1], andisheh=[0, 2]}"
In the same watch list when I enter:
invertedList.get("ryerson")
I get null as result, also in the code. As you can see "ryerson" is already there as a key in my invertedList and I should get [0, 2, 3] as a result!!! What is happening here? I'm so confused!
I know there is a problem with ArrayList as values, because I tested Integer as values and it worked fine, but still don't know how to solve it. I am new to java, used to work with C#.
The complete code of invertedList:
public class InvertedIndex {
public HashMap<String, ArrayList<Integer>> invertedList;
public ArrayList<String> documents;
public InvertedIndex(){
invertedList = new HashMap<String, ArrayList<Integer>>();
documents = new ArrayList<String>();
}
public void buildFromTextFile(String fileName) throws IOException {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int documentId = 0;
while(true){
String line = bufferedReader.readLine();
if(line == null){
break;
}
String[] words = line.split("\\W+");
for (String word : words) {
word = word.toLowerCase();
if(!invertedList.containsKey(word))
invertedList.put(word, new ArrayList<Integer>());
invertedList.get(word).add(documentId);
}
documents.add(line);
documentId++;
}
bufferedReader.close();
}
The test code:
@Test
public void testBuildFromTextFile() throws IOException {
InvertedIndex invertedIndex = new InvertedIndex();
invertedIndex.buildFromTextFile("input.tsv");
Assert.assertEquals("{ryerson=[0, 2, 3], 23=[3], award=[1], andisheh=[0, 2]}", invertedIndex.invertedList.toString());
ArrayList<Integer> resultIds = invertedList.get("ryerson");
ArrayList<Integer> expectedResult = new ArrayList<Integer>();
expectedResult.add(0);
expectedResult.add(2);
expectedResult.add(3);
Assert.assertEquals(expectedResult, resultIds);
}
The first Assert works fine, the second one, resultIds is null.
If I'm reading this right, and assuming correctly, this test function is inside the InvertedIndex class. I only make that assumption because the line
ArrayList<Integer> resultIds = invertedList.get("ryerson");
should actually be uncompilable as there is no local variable called "invertedList".
That line should read
ArrayList<Integer> resultIds = invertedIndex.invertedList.get("ryerson");
这篇关于Hashmap get函数返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!