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>
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 TypeMethodDescriptionboolean
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
(Collection<? extends K> c) Adds all of the elements in the specified collection to this set, as if by callingadd(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
Returnstrue
if this collection contains the specified element.final boolean
containsAll
(Collection<?> c) Returnstrue
if this collection contains all of the elements in the specified collection.boolean
Compares the specified object with this collection for equality.getMap()
Returns the map backing this view.Returns the default mapped value for additions, ornull
if additions are not supported.int
hashCode()
Returns the hash code value for this collection.final boolean
isEmpty()
Returnstrue
if this collection contains no elements.iterator()
Returns an iterator over the elements in this collection.boolean
Removes the key from this map view, by removing the key (and its corresponding value) from the backing map.boolean
removeAll
(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation).final boolean
retainAll
(Collection<?> c) 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
(T[] a) 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods declared in interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods declared in interface java.util.Set
clear, containsAll, isEmpty, removeAll, retainAll, size, spliterator, toArray, toArray
-
Method Details
-
getMappedValue
Returns the default mapped value for additions, ornull
if additions are not supported.- Returns:
- the default mapped value for additions, or
null
if not supported
-
contains
Returnstrue
if this collection contains the specified element. More formally, returnstrue
if and only if this collection contains at least one elemente
such thatObjects.equals(o, e)
.- Specified by:
contains
in interfaceCollection<K>
- Specified by:
contains
in interfaceSet<K>
- Parameters:
o
- element whose presence in this collection is to be tested- Returns:
true
if this collection contains the specified element- Throws:
NullPointerException
- if the specified key is null
-
remove
Removes the key from this map view, by removing the key (and its corresponding value) from the backing map. This method does nothing if the key is not in the map.- Specified by:
remove
in interfaceCollection<K>
- Specified by:
remove
in interfaceSet<K>
- Parameters:
o
- the key to be removed from the backing map- Returns:
true
if the backing map contained the specified key- Throws:
NullPointerException
- if the specified key is null
-
iterator
Returns an iterator over the elements in this collection.The returned iterator is weakly consistent.
-
add
Adds the specified key to this set view by mapping the key to the default mapped value in the backing map, if defined.- Specified by:
add
in interfaceCollection<K>
- Specified by:
add
in interfaceSet<K>
- Parameters:
e
- key to be added- Returns:
true
if this set changed as a result of the call- Throws:
NullPointerException
- if the specified key is nullUnsupportedOperationException
- if no default mapped value for additions was provided
-
addAll
Adds all of the elements in the specified collection to this set, as if by callingadd(K)
on each one.- Specified by:
addAll
in interfaceCollection<K>
- Specified by:
addAll
in interfaceSet<K>
- Parameters:
c
- the elements to be inserted into this set- Returns:
true
if 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()Description copied from interface:Collection
Returns the hash code value for this collection. While theCollection
interface adds no stipulations to the general contract for theObject.hashCode
method, programmers should take note that any class that overrides theObject.equals
method must also override theObject.hashCode
method in order to satisfy the general contract for theObject.hashCode
method. In particular,c1.equals(c2)
implies thatc1.hashCode()==c2.hashCode()
. -
equals
Description copied from interface:Collection
Compares the specified object with this collection for equality.While the
Collection
interface adds no stipulations to the general contract for theObject.equals
, programmers who implement theCollection
interface "directly" (in other words, create a class that is aCollection
but is not aSet
or aList
) must exercise care if they choose to override theObject.equals
. It is not necessary to do so, and the simplest course of action is to rely onObject
's implementation, but the implementor may wish to implement a "value comparison" in place of the default "reference comparison." (TheList
andSet
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 ifb.equals(a)
). The contracts forList.equals
andSet.equals
state that lists are only equal to other lists, and sets to other sets. Thus, a customequals
method for a collection class that implements neither theList
norSet
interface must returnfalse
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 theSet
andList
interfaces.) -
getMap
Returns the map backing this view.- Returns:
- the map backing this view
-
clear
public final void clear()Removes all of the elements from this view, by removing all the mappings from the map backing this view.- Specified by:
clear
in interfaceCollection<K>
-
size
public final int size()Description copied from interface:Collection
Returns the number of elements in this collection. If this collection contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Specified by:
size
in interfaceCollection<K>
- Returns:
- the number of elements in this collection
-
isEmpty
public final boolean isEmpty()Description copied from interface:Collection
Returnstrue
if this collection contains no elements.- Specified by:
isEmpty
in interfaceCollection<K>
- Returns:
true
if this collection contains no elements
-
toArray
Description copied from interface:Collection
Returns an array containing all of the elements in this collection. 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. The returned array's runtime component type isObject
.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:
toArray
in 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) Description copied from interface:Collection
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.If 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 anynull
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:
toArray
in 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
Returns a string representation of this collection. The string representation consists of the string representations of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"
). Adjacent elements are separated by the characters", "
(comma and space). Elements are converted to strings as byString.valueOf(Object)
. -
containsAll
Description copied from interface:Collection
Returnstrue
if this collection contains all of the elements in the specified collection.- Specified by:
containsAll
in interfaceCollection<K>
- Parameters:
c
- collection to be checked for containment in this collection- Returns:
true
if this collection contains all of the elements in the specified collection- See Also:
-
removeAll
Description copied from interface:Collection
Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.- Specified by:
removeAll
in interfaceCollection<K>
- Parameters:
c
- collection containing elements to be removed from this collection- Returns:
true
if this collection changed as a result of the call- See Also:
-
retainAll
Description copied from interface:Collection
Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.- Specified by:
retainAll
in interfaceCollection<K>
- Parameters:
c
- collection containing elements to be retained in this collection- Returns:
true
if this collection changed as a result of the call- See Also:
-