1

I find myself in need of a data structure which only needs the ability to enter objects, retrieve and delete them (I have no interest in its size and etc..). This data structure will be accessed from multiple threads both adding and getting specific objects. Currently I am using in my program a concurrent HashMap:

key = object.hashCode()
data = object

The question: I was wondering this is the best way to go ?

Note:

  • I do not add any object twice

  • the only hashMap methods i use are get, put, remove.

Thanks alot!

Gal Sheldon
  • 83
  • 1
  • 8

2 Answers2

0

Have a look at java.util.concurrent.atomic package.

A small toolkit of classes that support lock-free thread-safe programming on single variables.

AtomicReference is one solution.

Refer to related SE question:

When to use AtomicReference in Java?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
-1

You could use a HashTable what is a thread safe version of HashMap. https://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html

shalama
  • 1,133
  • 1
  • 11
  • 25