I have a list of files. I would like to scan through and keep a count of the number of files with the same size. the issue is with filesize which is a long, as we know, hashmap will take in only an object and not a primitive. So using new Long(filesize)
, I put it into the hashmap. instead of getting a pair of (filesize, count), I got a list of (filesize, 1) due to the fact that each Long obj is unique.
How do I go about building this accumulator?
Any solution for 1.4.2?
You simply do it this way:
Map<Long, Integer> count = new HashMap<Long, Integer>();
for (File file : files) {
long size = file.getTotalSpace();
Integer n = count.get(size);
if (n == null) {
count.put(size, 1);
} else {
count.put(size, n + 1);
}
}
There is some auto-boxing and unboxing going on here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With