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
0
votes
1 answer

How to wait until a WeakHashMap has updated?

Is there another way besides a fixed timeout to know when a WeakHashMap has updated its entries after a key becomes weakly reachable? For example this code which nulls the strong reference to the key still prints that the entry is in the map unless…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
1 answer

Java SoftHashMap Implementation

I wanted to implement SoftHashMap based on Java SoftReference and HashMap. Java docs, about WeakHashMap, say that keys are weak references rather than values. I was wondering what hashcode() would be used for put and pull functions of the underlying…
Abidi
  • 7,846
  • 14
  • 43
  • 65
0
votes
5 answers

In weakHashMap if i update or change the previously stored key object, the garbage Collector deletes the key value pair

I have stored an object as a key in WeakHashMap. Now if I change its value and then call the GC and print map, then nothing is there. public static void main(String[] args) throws InterruptedException{ WeakHashMap map = new WeakHashMap(); …
sonu kumar
  • 143
  • 1
  • 8
0
votes
1 answer

How do I convert nested arraylist to an arraylist?

I have stored HashMap into an ArrayList and now I want each position of ArrayList in different array so please can somebody tell me how it is possible? My array list is like this: {key:value, key:value, key:value, key:value}, {key:value,…
0
votes
2 answers

Avoiding boilerplate in Java interfaces

I'm trying to build a key-value store with stored procedures and I've found that enums come in handy for defining things. I'd like a DB to be an enum of Tables, and a Table is associated with an enum of Operations and an enum of Regions. The problem…
arachnivore
  • 147
  • 1
  • 7
0
votes
3 answers

How WeakHashMap works under the hood

I invesigate WeakHashMap ource code to have more knowledge about WeakReference I have found that entry looks like this: private static class Entry extends WeakReference implements Map.Entry { V value; final int…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0
votes
1 answer

Cache Invalidation while Getting from WeakHashMap

I cache database connection objects via WeakHashMap as like: Connection conn; if (connectionCache.get(jdbc.getConnectionURL()) == null) { conn = DriverManager.getConnection(jdbc.getConnectionURL(), jdbc.getUsername(),…
kamaci
  • 72,915
  • 69
  • 228
  • 366
0
votes
1 answer

why garbage collector does not destroying any object as a value in WeakHashMap which doesn't have any refrence?

In below example, garbage collector is destroying an useless (refrence less) object if it is used as key in weakHashMap, which is alright.. but if any useless object as value, than why garbage collector is not destroying that object.. public…
0
votes
2 answers

use WeakHashMap in a method or not?

I have a @Scheduled method that is called each 5 minutes. The batch does a lot of calculation, it takes about 2-15 minutes to complete (there is a flag that prevents a batch to run if one is already running). I use lot of Maps to cache the results…
0
votes
2 answers

Why is WeakReferenced object not removed by WeakHashMap

I recently tried to understand java.util.WeakHashMap. But when I use WeakReference to wrap a String, the WeakHash isn't finalizing the Entry. Also note that I am clearing the WeakReference in the main thread before it is been referred inside the…
Maas
  • 1,317
  • 9
  • 20
0
votes
0 answers

Android: working with interface and WeakHashMap

After a whole night spent in test (without any luck) I need some support with my interface. I'm working directly on the Android frameworks and I created a class that works as a Binder with a WeakHashMap to control the callbacks. Here is the…
iGio90
  • 3,251
  • 7
  • 30
  • 43
0
votes
1 answer

How to prevent a value from strongly referring to its key?

A much better programmer than me informed me that he holds a map of his WebSocket connections with java-websocket in a WeakHashMap. I've looked into that map for myself and it seems perfect since a connection will close without the thread…
user1382306
0
votes
1 answer

What are WeakReferences, Weakhashmaps, softreferences used for?

Please explain what WeakReferences are used for. I usually do understand Java concepts, but this one is giving me trouble. I do understand what WeakReferences are, but their usage and nature is a little vague inside my head. I am not able to…
0
votes
1 answer

stack overflow on call to WeakHashMap when using xstream in android app

Why do i get a "Stack overflow on call to Ljava/util/WeakHashMap" error when xstream is converting the ShootRecord to xml? ShootRecord class fields and constructor public class ShootRecord /* implements Serializable */ { private String…
jsky
  • 2,225
  • 5
  • 38
  • 54
0
votes
3 answers

WeakHashMap Randomly clears

I am running a game, when I start up I load images into a WeakHashMap of Images. When I run my game, my RAM just keeps going up, then eventually my WeakHashMap just unload all of their data. Is this relates to Garbage Collection? Any solutions?
Solplex
  • 9
  • 2