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
3
votes
2 answers

Java: need advise about WeakHashMap

I guess I'm another person trying to make some kind of a cache with WeakHashMap. And I need some help with it. I have bunch of TrackData objects that contain information about audio tracks. Then there are Track objects that keep reference to the…
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
3
votes
1 answer

WeakReference. Objects are not removed when only WeakReferences on it left

Inside our system we present session of client as class Session. Historically hashcode of this class is mutable - it is 0 on creation and change to user id at some point of time. There are existed two session managers in the system. Client…
3
votes
3 answers

Garbage collector work with 2 WeakHashMaps

I have cache, implemented with WeakHashMap, like this: private static WeakHashMap> objects = new WeakHashMap<>(); I have an instance of class City: City c = new City(); I now add this instance to my map like…
Andrey
  • 853
  • 9
  • 27
3
votes
0 answers

WeakHashMap hashing by identity?

I don't understand, why does WeakHashMap hashes keys by their hashCode() and identifies by equals()? By contract of this class, it can used to tie any instance to any other instance. From my opinion, this implies hashing and comparing by identity.…
Dims
  • 47,675
  • 117
  • 331
  • 600
3
votes
1 answer

WeakHashMap with types like Long, Int or String

While doing some researches about how to pass a object reference in android I was thinking about the following. Let's assume I have a WeakHashmap with Long as keys. And now I put one Object into this WeakHashMap and assign it to the key 'new…
Chris
  • 661
  • 2
  • 7
  • 23
2
votes
3 answers

How do I call "put" on a WeakHashMap in Kotlin?

I created a WeakHashMap in Kotlin and for some reason, I am unable to call put it, it won't resolve. val dataMap: Map = WeakHashMap() dataMap.put(myInt, myData) // doesn't resolve Is there a Kotlin equivalent for a…
VIN
  • 6,385
  • 7
  • 38
  • 77
2
votes
1 answer

WeakHashMap how is the entry _actually_ found after the reference is put on the ReferenceQueue

A WeakHashMap works pretty much as a WeakReference coupled with a ReferenceQueue - there is zero news about this. Here is a stripped down example of how it is supposed to work: public class ReferenceQueuePlayground { public static void…
Eugene
  • 117,005
  • 15
  • 201
  • 306
2
votes
2 answers

Is a non-synchronized WeakHashMap harmful?

I have a code look like this. private static Map PATTERNS; private static Map patterns() { if (PATTERNS == null) { PATTERNS = new WeakHashMap<>(); // ok? or should be synchronized? } return…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
2
votes
1 answer

Java WeakHashMap clean up

I try to figure out how the WeakHashMap cleans up after garbare collection. As many of you may know, the WeakHashMap entry is removed automatically when its key becomes garbage collected. But, for instance, if I do something like…
2
votes
4 answers

HashMap being garbage collected along with WeakHashMap?

As of my understanding HashMap should not be garbage collected and WeakHashMap should be garbage collected but When I'm running this code both hashmap and weakhashmap are being garbage collected. import java.util.HashMap; import…
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
2
votes
1 answer

WeakHashMap or HashMap?

So I have a chain of objects that reference each other from ORM / Hibernate Continent has many countries has many states has many cities has many cityparts Country has one Country has many states has many cities has many cityparts State has…
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
2
votes
2 answers

GC doesnt remove objects from weakhashmap

I hava a test application with one class that stores a map and a button that invokes method of that class: Map weakMap = new WeakHashMap(); The button does this: public void fillWeakHashMap(int size) { …
user1308908
2
votes
1 answer

Java Can I guarantee that WeakHashMap entry will not disappear after check

I want to store some data in WeakHashMap, but there is a problem. Say we have a code: public class WeakMapTest { public static void main(String[] args) { WeakHashMap map = new WeakHashMap(); …
DeGriz
  • 318
  • 3
  • 15
2
votes
3 answers

In-memory caching objects in java

I want to cache objects in memory. The requirements are as follows: Every record/object is associated with a unique key. 400-500 records/objects to be stored. If the number of records increase beyond the specified limit, then the older records…
Rakesh
  • 3,987
  • 10
  • 43
  • 68
1
vote
1 answer

WeakKeyDictionary with tuple of objects as key

I want to use a WeakKeyDictionary where the keys are tuples of other objects, e.g. of type Tuple[A,B], in such a way: # a,b,c defined somewhere d = WeakKeyDictionary() d[(a, b)] = c This does not work because: TypeError: cannot create weak…
Albert
  • 65,406
  • 61
  • 242
  • 386