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

  • Home
  • SEARCH
  • 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 774333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:06:52+00:00 2026-05-14T19:06:52+00:00

I need to define a weak reference Map, whose value is a Set. I

  • 0

I need to define a weak reference Map, whose value is a Set. I use Google collections’ MapMaker, like this:

Map<Class<? extends Object>, Set<Foo>> map = new MapMaker().weakKeys().weakValues().makeMap();

So, for Set<Foo>, can I use a normal HashSet? Or, do I have to create a weak HashSet, like this:

Collections.newSetFromMap(new WeakHashMap<Foo, Boolean>());

And why?

Another question, the key of my map is Class objects, when will a Class object become weakly reachable? In other words, what is the lifetime of a Class object?

Thanks.

Update: The 1st question is not really clear. My concern was that when I do map.get(key).add(foo) to add foo instance to the Set, will this also add a strong reference to foo instance and thus preventing it from being GCed? That’s why I was wondering if I should use a weak HashSet.

  • 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. Editorial Team
    Editorial Team
    2026-05-14T19:06:52+00:00Added an answer on May 14, 2026 at 7:06 pm

    To answer the second part first, a Class object is only eligible for garbage collection when the responsible Classloader is in turn garbage collectied. See Section 12.7 of the JLS:

    A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector as discussed in §12.6. Classes and interfaces loaded by the bootstrap loader may not be unloaded.

    So a WeakReference<Class> is probably not likely to do what you’re hoping for (though it’s not 100% clear to me what you are looking for).

    Now, back to the first part — do you need a weak HashSet? That depends, again, on what you’re trying to do. If you want the Map entries to be reclaimed when the value Set itself is no longer referenced, no, you don’t need a weak HashMap. (google-collections uses equality reference for weakly-referred values and keys, by the way, so there are no tricky equality issues here). Next GC after the last reference to the Set is discarded, the Map entry will be reclaimed.

    Note also that this will allow the Foos to be garbage-collected too; once you drop the reference to the Set<Foo>, the Foos are only weakly reachable and can be discarded. See the Javadoc for java.lang.ref package:

    An object is strongly reachable if it can be reached by some thread without traversing any reference objects. […] An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.

    So if the only chain of reference is (Strong reference to Map) → (Map holds weak reference to Set) → (Set holds strong reference to Foo) then the Foo can be garbage-collected.

    However, I have a nagging suspicion this isn’t what you’re after. I suspect what you want is for the Map entry to be reclaimed when the last Foo is no longer referenced; that you’re not holding a reference to the Set<Foo> itself, but rather the individual Foo objects.

    If that’s the case, then no, this won’t do what you want. What you really need is a weakly-valued Multimap — something like a MultimapMaker.weakValues(). However, there is no MultimapMaker at present: see guava-libraries issue #142 for a request to add this. Sorry!

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

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • 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 No, this is not an example of an Adapter Pattern,… May 16, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer You could do something like this, using .height() and .width()… May 16, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer A pair is implemented as a templated struct. It provides… May 16, 2026 at 3:51 pm

Trending Tags

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

Top Members

Related Questions

I need to define a class like this: class Color { private: union Data
I need to define a class that represents a real-life event -- like a
We need to define a conditional compilation symbol in a class library project. This
I need to define a calculated member in MDX (this is SAS OLAP, but
I need to define the constant in the module that use the method from
I need to define new UI Elements as well as data binding in code
I need to define an arbitrary comparison method for sqlite in code. I need
I need to define simple classes and interfaces ( Ex. IClassInterface ) in a
I want to define control styles in a resource dictionary and use those for
UPDATE - No need to answer this now, I have solved below. Hi, I'm

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.