aspose file tools*
The moose likes Java in General and the fly likes HashMap remove multiple elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap remove multiple elements" Watch "HashMap remove multiple elements" New topic
Author

HashMap remove multiple elements

Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Hi everyone. I have a small problem. I have a HashMap with integer keys. I need to remove elements that have the key smaller than a specified number. Can I do this in an optimized way without parsing the entire hashmap?

Thanks for your help
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19545
    
  16

Doua Beri wrote:Hi everyone. I have a small problem. I have a HashMap with integer keys. I need to remove elements that have the key smaller than a specified number. Can I do this in an optimized way without parsing the entire hashmap?

Switch to a TreeMap, then use headMap and clear:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4241
    
    7

With a normal HashMap I don't think you've got any better options than iterating through the Map, checking the key, and removing the ones you don't want. I'd suggest using the entrySet to do this. E.g.


It would be different if you had a SortedMap or NavigableMap (e.g. TreeMap). These are ordered according to their keys, and there are methods available to give you a view onto a sub-map, which you can then clear. E.g.


That won't be more efficient, but it's more concise. If you're not able to switch the type of map in the first place it's not much help to you, though, I'm afraid.
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Thanks for your replies. I can change the Map type. I just need to be optimized because I will have a large Map( around 10k elements) so I'm more interested in a faster, and less resource eater solution.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19545
    
  16

I don't think there is much difference in memory usage in both solutions. That leaves speed, and there is a trade-off there:
- TreeMap is slower for general purposes but is faster than HashMap for these specific deletions - if the number of items to remove is not too large
- HashMap is faster for general purposes. Peter's code is faster but requires more Integer objects. If these Integer objects stay below 128 that doesn't matter due to the cache used with auto-boxing. Matthew's code will most likely be the slowest, as it needs to inspect each element.

If you need speed in general, TreeMap is probably not the way to go. My guess is that in most cases Peter's code will be faster than Matthew's code.

However, I think that 10k elements is nothing for these kinds of collections. TreeMap's log(10k) for retrieval / removal / insertion is around 9 times slower than the direct access of HashMap, but I doubt you will even notice - the difference will be microseconds, maybe even nanoseconds. It would be a case of premature optimization, and in that case the easiest solution should be used: SortedMap with headMap and clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap remove multiple elements
 
Similar Threads
Collections
Iterators on HashMap
List of common elements
How to print the elements of an arraylist that contains a hashmap
Objects returned by normal get*() methods: references or copies ?