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{" +
   
Edit
Report