Saving a pair of primitives in Java HashMap

I have a list of files. I would like to view and save the number of files with the same size. the problem is with the file size, which is long, as we know, hashmap will only accept an object, not a primitive. Therefore, using new Long(filesize) , I put it in hashmap. instead of getting a pair (filesize, count), I got a list (filesize, 1) because each Long obj is unique.

How can I build this battery?

any solution for 1.4.2?

+6
java hashmap primitive pair
source share
6 answers

You just do this:

 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 are several auto-boxing and unboxing here.

+15
source share

Instead of using new Long(size) you should use Long.valueOf(size) . which will return the same long link that is internally cached, and should also improve performance (not that it will be visible unless you perform millions of these new Long() operations).

ps. only works for java 1.5 or higher

+7
source share

You can use Trove to store pairs (long, int) - TLongIntHashMap

+4
source share

or you can use AtomicInteger as a mutable integer.

 Map<Long, AtomicInteger> count = new HashMap<Long, AtomicInteger>(); for (File file : files) { long size = file.length(); // getTotalSpace() get the space consumed (eg a multiple of 8K) rather the actual file size. AtomicInteger n = count.get(size); if (n == null) { count.put(size, new AtomicInteger(1)); } else { n.getAndIncrement(); } } 
+3
source share

An extension of what cletus wrote.

His solution is excellent, except that it only stores every file size that you encounter and the number of files with that size. If you ever want to know which files that are this data structure will be useless to you, so I don't think the cletus solution is complete enough. Instead, I would do

 Map<Long, Collection<File>> count = new HashMap<Long, Collection<File>>(); for (File file : files) { long size = file.getTotalSpace(); Collection<File> c = count.get(size); if (c == null) { c = new ArrayList<File>(); //or whatever collection you feel comfortable with count.put(size, c); } c.add(file); } 

then you can get the number of files with c.size (), and you can easily iterate all the files with this number without having to perform this procedure again.

+1
source share

I think there is more to this and we will need more details from you. I assume that you know that there are definitely more than one file of a given size, otherwise I will first check that this happens. As far as you know, you just have a lot of files with unique file sizes.

You mentioned:

... because each Long obj is unique.

I do not think this is a problem. While this may be true depending on how you create Longs, this should not stop HashMaps from behaving the way you want. As long as the two key objects return the same hashCode () value and the equals () method says they are equal, your HashMap will not create another record for it. In fact, you will not be able to see the "list (filesize, 1)" with the same file size values ​​(unless you wrote your own Long and were unable to properly implement hashCode () / equals ()).

However, Cletus code should work if you use Java 5 or later, if you use Java 1.4 or lower, you need to either manually do your boxing / unboxing or look into the Apache Community Collections . Here's the pre-Java 5 version of the Cletus example:

 Map count = new HashMap(); for (Iterator filesIter = files.iterator(); filesIter.hasNext();) { File file = (File)filesIter.next(); long size = file.getTotalSpace(); Integer n = count.get(size); if (n == null) { count.put(size, Integer.valueOf(1)); } else { count.put(size, Integer.valueOf(n.intValue() + 1)); } } 
+1
source share

All Articles