java map.get返回null,Hashmap get函数返回null

I have a hashmap which is

public HashMap> 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> invertedList;

public ArrayList documents;

public InvertedIndex(){

invertedList = new HashMap>();

documents = new ArrayList();

}

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());

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 resultIds = invertedList.get("ryerson");

ArrayList expectedResult = new ArrayList();

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 resultIds = invertedList.get("ryerson");

should actually be uncompilable as there is no local variable called "invertedList".

That line should read

ArrayList resultIds = invertedIndex.invertedList.get("ryerson");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值