Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

HashMap remove multiple elements

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 22458
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 22458
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic