Questions tagged [weakhashmap]

A special data structure in Java, a WeakHashMap is a hashtable-based Map with weak keys, meaning when a key has been discarded its entry is effectively removed from the map.

Java's WeakHashMap is a hashtable-based Map with weak keys, meaning when a key has been discarded its entry is effectively removed from the map.

This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed.

77 questions
6
votes
2 answers

spring security, how to expire all sessions of a user

I have to solve the following scenario, in a Spring Security 3.2.5-RELEASE with Spring Core 4.1.2-RELEASE application running Java 1.7 on wildfly 8.1. user 'bob' logs in and Admin deletes 'bob' if 'bob' logs out, he can't log in. again but he`s…
Ilan.K
  • 673
  • 14
  • 22
6
votes
5 answers

Usage of WeakHashMap?

WeakHashMap is an implementation of Map interface where the memory of the value object can be reclaimed by Grabage Collector if the corresponding key is no longer referred by any section of program. So if key is no longer used in program. its…
M Sach
  • 33,416
  • 76
  • 221
  • 314
6
votes
2 answers

Logging when objects are garbage collected

My application logs the usage of certain objects - my setup uses AspectJ to identify the contexts I'm interested in and logs these usages. I later load the log files for analysis, but for efficiency reasons it's useful to know when an object is no…
selig
  • 4,834
  • 1
  • 20
  • 37
5
votes
4 answers

Can someone explain to me when it is useful to use MapMaker or WeakHashMaps?

I have read many people really like the MapMaker of Google Guava (Collections), however I cannot see any good uses of it. I have read the javadoc, and it says that it behaves like ConcurrentHashMap. It also says new MapMaker().weakKeys().makeMap()…
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
4
votes
2 answers

Java7 WeakHashMap isEmpty() seems wrong

I'm trying to use Java7's WeakHashMap and I found its isEmpty() method give me wrong results. import java.util.Map; import java.util.WeakHashMap; public class Test { public static void main(final String[] args) { final Map
Peng
  • 45
  • 4
4
votes
2 answers

Is WeakHashMap ever-growing, or does it clear out the garbage keys?

I am trying to use WeakHashMap as a concurrent Set of weak references. this.subscribers = Collections.synchronizedSet( Collections.newSetFromMap( new WeakHashMap <>() …
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4
votes
4 answers

How to know the value that was associated with a removed entry in a WeakHashMap

I've something like this: private Map map = new WeakHashMap(); ... somewhere in the code ... MyObj1 myObj1 = new MyObj1(); map.put(myObj1, new MyObj2(); ... myObj1 = null; ... somewhere else in a thread ...…
mickthompson
  • 389
  • 1
  • 4
  • 12
4
votes
2 answers

Java WeakHashMap Class

I wanted to test Java WeakHashMap Class functionality and for that matter I wrote the following test: public class WeakHashMapTest { public static void main(String args[]) { Map weakMap = new WeakHashMap<>(); String x = new…
4
votes
3 answers

Behaviour of weakhashmap with String literal and String object

I am understanding the concept of WeakhashMap. String literal and String object made it difficult to understand. Following is the code: package com.lnt.StringBuf; import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; public…
Sharp
  • 53
  • 1
  • 6
4
votes
6 answers

What is the purpose of WeakHashMap when there is HashMap and Concurrent HashMap?

What is the need arises for introducing Weak HashMap when there is already other implementations available. In short i have two issues : Why jdk has WeakHashMap when there is HashMap and Concurrent HashMap in java ? What is the use of it in…
Prateek
  • 12,014
  • 12
  • 60
  • 81
3
votes
2 answers

Java, convert object to softreference

I need to put a data object into my weakhashmap containing softreferences. How do I convert my "Drawable" object into a softreference? WeakHashMap > tempPulled = new WeakHashMap
CQM
  • 42,592
  • 75
  • 224
  • 366
3
votes
3 answers

WeakHashMap - what is its purpose and how should it be used correctly

Today I found this blog post which discussed usages of WeakHashMap over cache. It was intrigued by the fact that not the values, but the keys are stored as weak references, and when the reference is no more alive, the entire key-value pair is…
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
3
votes
4 answers

When will Java WeakHashMap clean null key?

In the code below nameRef.get() is null , after name = null and System.gc(). import java.lang.ref.WeakReference; public class Main { public static void main(String[] args) { String name = new String("ltt"); …
letiantian
  • 437
  • 2
  • 14
3
votes
3 answers

Weak Reference maintainability

I was reading up on weak references in java and sounds simple enough, if an object only has weak references on it, then it can be collected by the garbage collector. Except what happens if your reference becomes dead before you use the value?…
Fancypants753
  • 429
  • 1
  • 6
  • 19
3
votes
4 answers

Automatic removal of entries in WeakHashMap

There is a WeakHashMap instance initialized with, for example, 500 entries. Now, its keys have not been referenced anywhere in the running application for a day or so. Will this map's entries be removed automatically after a certain time gets…