- java.lang.Object
-
- java.util.concurrent.ConcurrentHashMap.KeySetView<K,V>
-
- All Implemented Interfaces:
Serializable,Iterable<K>,Collection<K>,Set<K>
- Enclosing class:
- ConcurrentHashMap<K,V>
public static class ConcurrentHashMap.KeySetView<K,V> extends Object implements Set<K>, Serializable
A view of a ConcurrentHashMap as aSetof keys, in which additions may optionally be enabled by mapping to a common value. This class cannot be directly instantiated. SeekeySet(),keySet(V),newKeySet(),newKeySet(int).- Since:
- 1.8
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(K e)Adds the specified key to this set view by mapping the key to the default mapped value in the backing map, if defined.booleanaddAll(Collection<? extends K> c)Adds all of the elements in the specified collection to this set, as if by callingadd(K)on each one.voidclear()Removes all of the elements from this view, by removing all the mappings from the map backing this view.booleancontains(Object o)Returnstrueif this collection contains the specified element.booleancontainsAll(Collection<?> c)Returnstrueif this collection contains all of the elements in the specified collection.booleanequals(Object o)Indicates whether some other object is "equal to" this one.voidforEach(Consumer<? super K> action)Performs the given action for each element of theIterableuntil all elements have been processed or the action throws an exception.ConcurrentHashMap<K,V>getMap()Returns the map backing this view.VgetMappedValue()Returns the default mapped value for additions, ornullif additions are not supported.inthashCode()Returns a hash code value for the object.booleanisEmpty()Returnstrueif this collection contains no elements.Iterator<K>iterator()Returns an iterator over the elements in this collection.booleanremove(Object o)Removes the key from this map view, by removing the key (and its corresponding value) from the backing map.booleanremoveAll(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).booleanretainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).intsize()Returns the number of elements in this collection.Spliterator<K>spliterator()Creates aSpliteratorover the elements in this set.Object[]toArray()Returns an array containing all of the elements in this collection.<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.StringtoString()Returns a string representation of this collection.-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
-
-
-
-
Method Detail
-
getMappedValue
public V getMappedValue()
Returns the default mapped value for additions, ornullif additions are not supported.- Returns:
- the default mapped value for additions, or
nullif not supported
-
contains
public boolean contains(Object o)
Returnstrueif this collection contains the specified element. More formally, returnstrueif and only if this collection contains at least one elementesuch thatObjects.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)
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:
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()
Returns an iterator over the elements in this collection.The returned iterator is weakly consistent.
-
add
public boolean add(K e)
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:
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 nullUnsupportedOperationException- if no default mapped value for additions was provided
-
addAll
public 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.- 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 arenullUnsupportedOperationException- if no default mapped value for additions was provided- See Also:
Set.add(Object)
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap.The general contract of
hashCodeis:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)method, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
Object.equals(java.lang.Object)method, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Objectdoes return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Specified by:
hashCodein interfaceCollection<K>- Specified by:
hashCodein interfaceSet<K>- Overrides:
hashCodein classObject- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
equals
public boolean equals(Object o)
Description copied from class:ObjectIndicates whether some other object is "equal to" this one.The
equalsmethod implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
The
equalsmethod for classObjectimplements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesxandy, this method returnstrueif and only ifxandyrefer to the same object (x == yhas the valuetrue).Note that it is generally necessary to override the
hashCodemethod whenever this method is overridden, so as to maintain the general contract for thehashCodemethod, which states that equal objects must have equal hash codes.- Specified by:
equalsin interfaceCollection<K>- Specified by:
equalsin interfaceSet<K>- Overrides:
equalsin classObject- Parameters:
o- the reference object with which to compare.- Returns:
trueif this object is the same as the obj argument;falseotherwise.- See Also:
Object.hashCode(),HashMap
- It is reflexive: for any non-null reference value
-
spliterator
public Spliterator<K> spliterator()
Description copied from interface:SetCreates aSpliteratorover the elements in this set.The
SpliteratorreportsSpliterator.DISTINCT. Implementations should document the reporting of additional characteristic values.- Specified by:
spliteratorin interfaceCollection<K>- Specified by:
spliteratorin interfaceIterable<K>- Specified by:
spliteratorin interfaceSet<K>- Returns:
- a
Spliteratorover the elements in this set
-
forEach
public void forEach(Consumer<? super K> action)
Description copied from interface:IterablePerforms the given action for each element of theIterableuntil all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.
-
getMap
public ConcurrentHashMap<K,V> 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:
clearin interfaceCollection<K>
-
size
public final int size()
Description copied from interface:CollectionReturns the number of elements in this collection. If this collection contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Specified by:
sizein interfaceCollection<K>- Returns:
- the number of elements in this collection
-
isEmpty
public final boolean isEmpty()
Description copied from interface:CollectionReturnstrueif this collection contains no elements.- Specified by:
isEmptyin interfaceCollection<K>- Returns:
trueif this collection contains no elements
-
toArray
public final Object[] toArray()
Description copied from interface:CollectionReturns 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 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.
This method acts as bridge between array-based and collection-based APIs.
- Specified by:
toArrayin interfaceCollection<K>- Returns:
- an array containing all of the elements in this collection
-
toArray
public final <T> T[] toArray(T[] a)
Description copied from interface:CollectionReturns 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 anynullelements.)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.
Like the
Collection.toArray()method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.Suppose
xis a collection known to contain only strings. The following code can be used to dump the collection into a newly allocated array ofString:String[] y = x.toArray(new String[0]);Note thattoArray(new Object[0])is identical in function totoArray().- Specified by:
toArrayin interfaceCollection<K>- Type Parameters:
T- the runtime 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()
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
public final boolean containsAll(Collection<?> c)
Description copied from interface:CollectionReturnstrueif 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:
Collection.contains(Object)
-
removeAll
public boolean removeAll(Collection<?> c)
Description copied from interface:CollectionRemoves 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:
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:
Collection.remove(Object),Collection.contains(Object)
-
retainAll
public final boolean retainAll(Collection<?> c)
Description copied from interface:CollectionRetains 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:
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:
Collection.remove(Object),Collection.contains(Object)
-
-