我正在创建一个wordcount程序,我在其中创建N
线程,并且每个线程都收到一个char[]
包含许多不同数字的缓冲区,如[2355 3326 94438 123 123...]
我想创建一个映射,其中键是数字本身,而价值是它出现了多少时间。我将从char[]
数组转换为一个整数,如下所示。putIfAbsent总是返回null
但是,似乎每次我拨打putIfAbsent()
时,它总是返回null
,这意味着它没有找到该值的关键字。这没有意义,因为我的文本文件中有数千个重复值。该地图最终应该在300kb左右,而不是一个千兆字节。
newbyte[]
是char[]
只包含数字和空格
为什么始终的putIfAbsent返回null?
此外,当我打印的地图完成后,它看起来像这样:
233303192 = 1
1770057208 = 1
1323329638 = 1
50 = 1
962422124 = 1
472527478 = 1
936125441 = 1
-350637153 = 1
-601349585 = 1
这是很奇怪的,因为任何输入的最大值为65535不知道如何这使得任何意义。
public void run() {
int counter = 0; int i; Integer check; int j =0; int temp = 0; int intbuilder = 0;
for (i = 0; i < newbyte.length; i++) {
if (newbyte[i] != ' ') { //delimiter is not found, so add to temp char array
intbuilder = (intbuilder * 10) + (int)newbyte[i];
counter++;
}
else {
check = wordCountMap.putIfAbsent(intbuilder, 1);
if (check != null) {
wordCountMap.put(intbuilder, check + 1);
}
intbuilder = 0;
也许不是你的问题,但不是'(INT)newbyte [I]'你可能意味着'(INT)(newbyte [I] - '0')'。 – OldCurmudgeon
你可能是对的,但它没有帮助。顺便说一下,我的映射是ConcurrentHashMap –
DanGordon