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
1
vote
1 answer

Can I use WeakHashMap for cached fields of classes?

For some reason, I need to cache entries of classes and their field or field name using reflection. private static final Map, String> ID_ATTRIBUTE_NAMES = new WeakHashMap<>(); private static String findIdAttributeName(final…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
0 answers

Confuse about java.util.WeakHashMap behavior?

Here is three cases, Why case1 result is {name=Bob}, however case2 result is {}, case3 is still {name=Bob}. the different between case1 and case2 is putting different key. run at JDK8. Why? //Case1: WeakHashMap mapCase1 = new…
LiLi
  • 391
  • 3
  • 11
1
vote
1 answer

Java - HashMap and WeakHashMap references used in Application

Just trying to understand something from GC viewpoint public Set returnFromDb(String id) { LookupService service = fromSomewhere(); Map where = new WeakHashMap<>() {} where.put("id",id); return…
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
1
vote
2 answers

Why my WeakHashMap entry doesn't be removed by GC?

I have a class to manage lock objects of several features. I've figured out that WeakHashMap should fit my requirement. Here is my code: public static class FeatureLockManager { private final Map locks; …
1
vote
3 answers

Java WeakHashMap with immutable key

I want to use a WeakHashMap for objects that will exist in memory for a short time. Each object has an id (unique integer field which is a primary key from DB), so my 1st thought was to use that field as the key for the object. However, Integer is…
Lior Tamir
  • 53
  • 4
1
vote
0 answers

Need to check how much memory my hash table has taken after inserting 1 million key values

I am using khash.h library for hashing. I want to check how much memory it has consumed after inserting my 1 million keys. Here is the code. https://github.com/attractivechaos/klib/blob/master/khash.h What I want: I am entering n unique entry in…
hunt009
  • 21
  • 2
1
vote
0 answers

Java Map with Threads as keys like WeakHashMap?

In a library, I have some ThreadLocal variable with some objects. I want to accumulate all these objects in one data structure and be able to iterate over them when needed. One the other hand, I don't want this data structure get polluted…
leventov
  • 14,760
  • 11
  • 69
  • 98
1
vote
1 answer

WeakHashMap not working as expected in observer pattern

I' am trying to implement observer pattern in Android with WeakHashMap. Here code. public class DataObservable { static WeakHashMap observers = new WeakHashMap<>(); public static void addObserver(DataObserver observer,…
1
vote
0 answers

Performance/Memory optimization with hashmap and weakhasmap

I have a question related to hashmap and weakhashmap.In my application, I will fetch 100k records from DB every time and put it in a map. After I do some manipulations with first set of records, I will fetch next 100k records. In this situation,…
Java P
  • 2,241
  • 6
  • 31
  • 45
1
vote
2 answers

Reduce Memory Usage With WeakHashMap

In the Javadoc of WeakHashMap.html, it said "Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. Therefore a key will automatically be removed only after the weak references to it, both inside and…
michael
  • 106,540
  • 116
  • 246
  • 346
1
vote
4 answers

ConcurrentModificationException with WeakHashMap

I have the code below but I'm getting ConcurrentModificationException, how should I avoid this issue? (I have to use WeakHashMap for some reason) WeakHashMap data = new WeakHashMap(); // some initialization code for…
Green Ho
  • 881
  • 3
  • 14
  • 27
1
vote
2 answers

Recreate the same key on WeakHashMap

According to Java doc for weakhashmap: "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…
Dayerman
  • 3,973
  • 6
  • 38
  • 54
1
vote
3 answers

Java: why does WeakHashMap implement Map whereas it is already implemented by AbstractMap?

Possible Duplicate: Java.util.HashMap — why HashMap extends AbstractMap and implement Map? Why would both a parent and child class implement the same interface? WeakHashMap is declared to both extend AbstractMap and implement…
Oz Molaim
  • 2,016
  • 4
  • 20
  • 29
0
votes
1 answer

How to clone/copy WeakHashMap? (Deep copy)

I had a HashMap and I used the clone function to make a deep copy. But I switched this to a WeakHashMap to try out some memory management. I still want to do this clone function but it isn't part of the methods in WeakHashMap. How do I efficiently…
CQM
  • 42,592
  • 75
  • 224
  • 366
0
votes
1 answer

How can I cache where a value has a strong reference to its key?

I have class whose instances can create a self-wrapped copy of its own. class Some { static class Nullable extends Some { Nullable(Some wrapped) { this.wrapped = wrapped; } @Override public void doSome() {} final Some…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184