If you want a Set<K> that is supported by ConcurrentHashMap , you should use Collections.newSetFromMap , for example
final Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>());
Now, if you really want to invent a wheel and care about memory usage, I suggest you just use a simple Object as your value. Since every object in Java inherits from Object (a universal base class), the size of any object in memory must be greater than or equal to the size of a simple Object . You cannot use primitives, since the general type arguments must be Object s.
EDIT: actually allocating a specific object to use as your value here will require more memory than using a pre-existing object, which is likely to be allocated for any paths. You can simply use a reference to an object that will more or less always be allocated during VM initialization, for example. Object.class . I really suggest you use only the first solution.
oldrinb
source share