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 8064597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:25:02+00:00 2026-06-05T11:25:02+00:00

I’m not used to deal with Soft and Weak references in Java but i

  • 0

I’m not used to deal with Soft and Weak references in Java but i understand the principle since i’m used to deal with datagrids like Gemfire, which provide overflow to hdd features when memory is full, probably using soft references or something similar i guess.

What i don’t understand in Guava is that it provides methods to make the keys soft/weak, and the values soft/weak.

I just wonder what’s the point of creating soft keys with non-soft values for exemple?
I mean, when the soft references start to be collected, we can’t find anymore the entry by its key, so why would we like the values to stay in the map?

Can someone give us some usecases with:

  • Weak key / soft values
  • Weak key / normal values
  • Soft key / weak values
  • Soft key / normal values

Thanks


Edit
I’m not sure my question is precise enough so what i’d like to know is:

  • when a key is collected (weak/soft), what happens to the value (non weak/soft)
  • when a value is collected (weak/soft), what happens to the key
  • Are entries with a missing part (key or value) kept in the cache?
  • And is there any usecase when you want such entries to stay in the cache.

Edit:
As discussed on Kevin Bourillon’s answer, finally i think i understand why using soft keys doesn’t mean anything.
Here’s why:

static class KeyHolder {
    final private String key;
    public KeyHolder(String key) {
        this.key = key;
    }
    public String getKey() {
        return key;
    }
    @Override
    public boolean equals(Object o) {
        KeyHolder that = (KeyHolder)o;
        boolean equality = this.getKey().equals(that.getKey());
        return equality;
    }
    @Override
    public int hashCode() {
        return key != null ? key.hashCode() : 0;
    }
    @Override
    public String toString() {
        return "KeyHolder{" +
                "key='" + key + '\'' +
                '}';
    }
}

public static void main(String[] args) {
    System.out.println("TESTING WEAK KEYS");
    testMap( new MapMaker().weakKeys().<KeyHolder,String>makeMap() );


    System.out.println("\n\n");
    System.out.println("TESTING SOFT KEYS");
    testMap(new MapMaker().softKeys().<KeyHolder, String>makeMap());


    System.out.println("\n\n");
    System.out.println("TESTING SOFT REFERENCES");
    KeyHolder key1 = new KeyHolder("toto");
    KeyHolder key2 = new KeyHolder("toto");
    SoftReference<KeyHolder> softRef1 = new SoftReference<KeyHolder>(key1);
    SoftReference<KeyHolder> softRef2 = new SoftReference<KeyHolder>(key2);
    System.out.println( "equals keys? " + key1.equals(key2) );
    System.out.println( "equals ref? " + softRef1.equals(softRef2) );
}

private static void testMap(Map<KeyHolder,String> map) {
    KeyHolder strongRefKey = new KeyHolder("toto");
    KeyHolder noStrongRefKey = new KeyHolder("tata");
    map.put(strongRefKey,"strongRef");
    map.put(noStrongRefKey,"noStrongRefKey");
    // we replace the strong reference by another key instance which is equals
    // this could happen for exemple in case of serialization/deserialization of the key
    noStrongRefKey = new KeyHolder("tata");
    System.gc();
    System.out.println( "strongRefKey = " + map.get(strongRefKey) );
    System.out.println( "noStrongRefKey = " + map.get(noStrongRefKey) );
    System.out.println( "keyset = " + map.keySet() );
}

This code produces the output:

TESTING WEAK KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='toto'}]



TESTING SOFT KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='tata'}, KeyHolder{key='toto'}]



TESTING SOFT REFERENCES
toto == toto -> true
equals keys? true
equals ref? false

As you can see, with the (deprecated) soft keys map, the KeyHolder containing “tata” still exists in the map.
But notice that i’m still not able to find my entry with a newly created key “new KeyHolder("tata");”
This is because, my keys are meaningfully equals, but the reference wrappers around them are not equals because their equals method is not overriden in Guava!
In this case, yes, softKeys doesn’t mean anything since you absolutly need to keep an identity reference to that key to be able to retrieve it.

  • 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-06-05T11:25:04+00:00Added an answer on June 5, 2026 at 11:25 am

    softKeys never makes sense, so we removed the method. softValues is the only way soft refs make sense, assuming the value instances aren’t also reachable in other ways outside the cache.

    Then usage of weakKeys basically boils down to whether you want identity equality for keys. If the key overrides equals and you need that equality behavior, you can’t use it. If you want identity, then weakKeys is how you get that, and it also makes sense because once all other references to the key have been GC’d, there would be no way to look up that entry anyway, so it might as well be removed.

    I actually am not entirely clear on when weakValues is useful, and was going to look into it. It would probably be a case where weakKeys is not an option (say, Integer keys), and where the values are ordinarily strongly referenced through other means, like some sort of session object, but when that object goes away, it signifies that no one will be looking for this in the cache anymore. It seems slightly farfetched when I put it that way, though.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.