Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 157047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:26:04+00:00 2026-05-11T10:26:04+00:00

Quick background I have a concurrent map I used to cache some values that

  • 0

Quick background I have a concurrent map I used to cache some values that change quite often (still worth caching them from testing). I want to evict items from my cache at regular intervals by examining an expire time in the value. I am using the keySet() method to get a reference to all my keys and then check the values and if expired i remove them. In other threads the cache is being queried and updated(removed) constantly.

From the javadocs for keySet() it mentioned if the map changes while I am iterating over the keyset Set the results are undefined. Obviously I would like a defined way of dealing with this so the results are valid. Would it be enough to pass the Set to a HashSet and then iterate over this set as it is my understanding this set will not be backed by the map, is this a wasteful of memory way of doing it? Any ideas appreciated.

Unfortunately my evictor is not the only way for items to be removed from the concurrent map so am I correct in saying that I need to copy the keySet to another Set before iterating through it.

Thanks in advance

EDIT: It turns out I was reading the javadocs for the Map keySet() method instead of the ConcurrentMap keySet(). Thanks my bad 🙂

Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:26:04+00:00Added an answer on May 11, 2026 at 10:26 am

    Are you using the Java Concurrent HashMap ? From the keySet() documentation it appears to behave in a fashion which you’d find useful.

    Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations. The view’s returned iterator is a ‘weakly consistent’ iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

    i.e. you can remove stuff and you should be fine.

    If you’re not using this implementation, then which are you using (and why ? Not being funny, but it would be interesting to know why you’ve made that choice)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 199k
  • Answers 199k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer JSON is typically a more efficient data encoding method than… May 12, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer If you mean the physical browser window itself, no. Javascript… May 12, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer Here's good article discussing the issue. I also have seen… May 12, 2026 at 7:41 pm

Related Questions

Quick background: I have a string which contains references to other pages. The pages
Quick background: I'm programming in PHP, I have a domain model with a separate
In OS3.0 a lot of plain table views have an initially hidden search bar
When encoding a java String to Latin-1 (ie. charset ISO-8859-1) I currently convert the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.