Class ConcurrentHashMap.KeySetView<K,V>
- Type Parameters:
K- the type of keysV- the type of values in the backing map
- All Implemented Interfaces:
Serializable, Iterable<K>, Collection<K>, Set<K>
- Enclosing class:
ConcurrentHashMap<K,V>
public static final class ConcurrentHashMap.KeySetView<K,V> extends Object implements Set<K>, Serializable
Set of keys, in which additions may optionally be enabled by mapping to a common value. This class cannot be directly instantiated. See keySet(), keySet(V), newKeySet(), newKeySet(int).- Since:
- 1.8
- See Also:
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
add |
Adds the specified key to this set view by mapping the key to the default mapped value in the backing map, if defined. |
boolean |
addAll |
Adds all of the elements in the specified collection to this set, as if by calling add(K) on each one. |
final void |
clear() |
Removes all of the elements from this view, by removing all the mappings from the map backing this view. |
boolean |
contains |
Returns true if this collection contains the specified element. |
final boolean |
containsAll |
Returns true if this collection contains all of the elements in the specified collection. |
boolean |
equals |
Compares the specified object with this collection for equality. |
ConcurrentHashMap |
getMap() |
Returns the map backing this view. |
V |
getMappedValue() |
Returns the default mapped value for additions, or null if additions are not supported. |
int |
hashCode() |
Returns the hash code value for this collection. |
final boolean |
isEmpty() |
Returns true if this collection contains no elements. |
Iterator |
iterator() |
Returns an iterator over the elements in this collection. |
boolean |
remove |
Removes the key from this map view, by removing the key (and its corresponding value) from the backing map. |
boolean |
removeAll |
Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
final boolean |
retainAll |
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
final int |
size() |
Returns the number of elements in this collection. |
final Object[] |
toArray() |
Returns an array containing all of the elements in this collection. |
final <T> T[] |
toArray |
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
final String |
toString() |
Returns a string representation of this collection. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods declared in interface Collection
parallelStream, removeIf, stream, toArray
Methods declared in interface Set
clear, containsAll, isEmpty, removeAll, retainAll, size, spliterator, toArray, toArray
Method Details
getMappedValue
public V getMappedValue()
null if additions are not supported.- Returns:
- the default mapped value for additions, or
nullif not supported
contains
public boolean contains(Object o)
true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that Objects.equals(o, e).- Specified by:
-
containsin interfaceCollection<K> - Specified by:
-
containsin interfaceSet<K> - Parameters:
-
o- element whose presence in this collection is to be tested - Returns:
-
trueif this collection contains the specified element - Throws:
-
NullPointerException- if the specified key is null
remove
public boolean remove(Object o)
- Specified by:
-
removein interfaceCollection<K> - Specified by:
-
removein interfaceSet<K> - Parameters:
-
o- the key to be removed from the backing map - Returns:
-
trueif the backing map contained the specified key - Throws:
-
NullPointerException- if the specified key is null
iterator
public Iterator<K> iterator()
The returned iterator is weakly consistent.
add
public boolean add(K e)
- Specified by:
-
addin interfaceCollection<K> - Specified by:
-
addin interfaceSet<K> - Parameters:
-
e- key to be added - Returns:
-
trueif this set changed as a result of the call - Throws:
-
NullPointerException- if the specified key is null -
UnsupportedOperationException- if no default mapped value for additions was provided
addAll
public boolean addAll(Collection<? extends K> c)
add(K) on each one.- Specified by:
-
addAllin interfaceCollection<K> - Specified by:
-
addAllin interfaceSet<K> - Parameters:
-
c- the elements to be inserted into this set - Returns:
-
trueif this set changed as a result of the call - Throws:
-
NullPointerException- if the collection or any of its elements arenull -
UnsupportedOperationException- if no default mapped value for additions was provided - See Also:
hashCode
public int hashCode()
CollectionCollection interface adds no stipulations to the general contract for the Object.hashCode method, programmers should take note that any class that overrides the Object.equals method must also override the Object.hashCode method in order to satisfy the general contract for the Object.hashCode method. In particular, c1.equals(c2) implies that c1.hashCode()==c2.hashCode().equals
public boolean equals(Object o)
Collection While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object's implementation, but the implementor may wish to implement a "value comparison" in place of the default "reference comparison." (The List and Set interfaces mandate such value comparisons.)
The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)
getMap
public ConcurrentHashMap<K,V> getMap()
- Returns:
- the map backing this view
clear
public final void clear()
- Specified by:
-
clearin interfaceCollection<K>
size
public final int size()
CollectionInteger.MAX_VALUE elements, returns Integer.MAX_VALUE.- Specified by:
-
sizein interfaceCollection<K> - Returns:
- the number of elements in this collection
isEmpty
public final boolean isEmpty()
Collectiontrue if this collection contains no elements.- Specified by:
-
isEmptyin interfaceCollection<K> - Returns:
-
trueif this collection contains no elements
toArray
public final Object[] toArray()
CollectionObject. The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
- Specified by:
-
toArrayin interfaceCollection<K> - Returns:
- an array, whose runtime component type is
Object, containing all of the elements in this collection
toArray
public final <T> T[] toArray(T[] a)
CollectionIf this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
- Specified by:
-
toArrayin interfaceCollection<K> - Type Parameters:
T- the component type of the array to contain the collection- Parameters:
-
a- the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. - Returns:
- an array containing all of the elements in this collection
toString
public final String toString()
"[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).containsAll
public final boolean containsAll(Collection<?> c)
Collectiontrue if this collection contains all of the elements in the specified collection.- Specified by:
-
containsAllin interfaceCollection<K> - Parameters:
-
c- collection to be checked for containment in this collection - Returns:
-
trueif this collection contains all of the elements in the specified collection - See Also:
removeAll
public boolean removeAll(Collection<?> c)
Collection- Specified by:
-
removeAllin interfaceCollection<K> - Parameters:
-
c- collection containing elements to be removed from this collection - Returns:
-
trueif this collection changed as a result of the call - See Also:
retainAll
public final boolean retainAll(Collection<?> c)
Collection- Specified by:
-
retainAllin interfaceCollection<K> - Parameters:
-
c- collection containing elements to be retained in this collection - Returns:
-
trueif this collection changed as a result of the call - See Also:
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.KeySetView.html