java - can I use WeakHashMap instead of HashMap -


i going through weakhashmap in java. , have understood weakhashmap hashmap except key references weakreference. means key references eligible gc, , when garbaged, entry removed map. not available in hashmap. please correct me if wrong.

i have 1 question on here.

now in future if requirement have use map putting key , value,can go forward weakhashmap? or need consider scenario weakhashmap wont fit hashmap fit?

you need consider context decide whether correct / safe use weakhashmap.

here's example weakhashmap not work (pseudo-code)

map<name, details> map = ... ever:     name = name user     if lookup:         details = map.get(name)         display details     else if create:         details = details user         map.add(name, details) 

with weakhashmap, there risk entries drop out of table, , user's lookups fail. hashmap, there no risk.


there issue weakreference , built on more expensive ordinary references. use more space , time. more significantly, reference classes incur overheads each time gc encounters them, can increase gc pause times.

however, issue of runtime overheads should normally secondary correctness issue.

  • if use hashmap when should using weakhashmap, liable run problems heap filling up. , has performance issues too.

  • if use weakhashmap should using hashmap, may loose information.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -