Package java.util.concurrent
Class ConcurrentHashMap.KeySetView<K,V>
- 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),- 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)Tests whether thisCollectioncontains the specified object.booleancontainsAll(Collection<?> c)Tests whether thisCollectioncontains all objects contained in the specifiedCollection.booleanequals(Object o)Compares this instance with the specified object and indicates if they are equal.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 an integer hash code for this object.booleanisEmpty()Returns if thisCollectioncontains no elements.Iterator<K>iterator()Returns a "weakly consistent" iterator that will never throwConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.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 occurrences in thisCollectionof each object in the specifiedCollection(optional).booleanretainAll(Collection<?> c)Removes all objects from thisCollectionthat are not also found in theCollectionpassed (optional).intsize()Returns a count of how many objects thisCollectioncontains.Object[]toArray()Returns a new array containing all elements contained in thisCollection.<T> T[]toArray(T[] a)Returns an array containing all elements contained in thisCollection.StringtoString()Returns a string representation of this collection.
-
-
-
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)
Tests whether thisCollectioncontains the specified object. Returnstrueif and only if at least one elementelemin thisCollectionmeets following requirement:(object==null ? elem==null : object.equals(elem)).- Specified by:
containsin interfaceCollection<K>- Specified by:
containsin interfaceSet<K>- Parameters:
o- the object to search for.- Returns:
trueif object is an element of thisCollection,falseotherwise.- 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 a "weakly consistent" iterator that will never throwConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
-
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
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Specified by:
hashCodein interfaceCollection<K>- Specified by:
hashCodein interfaceSet<K>- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object o)
Description copied from class:ObjectCompares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Specified by:
equalsin interfaceCollection<K>- Specified by:
equalsin interfaceSet<K>- Overrides:
equalsin classObject- Parameters:
o- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
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>- See Also:
Collection.isEmpty(),Collection.size()
-
size
public final int size()
Description copied from interface:CollectionReturns a count of how many objects thisCollectioncontains.- Specified by:
sizein interfaceCollection<K>- Returns:
- how many objects this
Collectioncontains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in thisCollection.
-
isEmpty
public final boolean isEmpty()
Description copied from interface:CollectionReturns if thisCollectioncontains no elements.- Specified by:
isEmptyin interfaceCollection<K>- Returns:
trueif thisCollectionhas no elements,falseotherwise.- See Also:
Collection.size()
-
toArray
public final Object[] toArray()
Description copied from interface:CollectionReturns a new array containing all elements contained in thisCollection. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. The array returned does not reflect any changes of theCollection. A new array is created even if the underlying data structure is already an array.- Specified by:
toArrayin interfaceCollection<K>- Returns:
- an array of the elements from this
Collection.
-
toArray
public final <T> T[] toArray(T[] a)
Description copied from interface:CollectionReturns an array containing all elements contained in thisCollection. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than thisCollection, the array element following theCollectionelements is set to null. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them.toArray(new Object[0])behaves exactly the same way astoArray()does.- Specified by:
toArrayin interfaceCollection<K>- Parameters:
a- the array.- Returns:
- an array of the elements from 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:CollectionTests whether thisCollectioncontains all objects contained in the specifiedCollection. If an elementelemis contained several times in the specifiedCollection, the method returnstrueeven ifelemis contained only once in thisCollection.- Specified by:
containsAllin interfaceCollection<K>- Parameters:
c- the collection of objects.- Returns:
trueif all objects in the specifiedCollectionare elements of thisCollection,falseotherwise.
-
removeAll
public final boolean removeAll(Collection<?> c)
Description copied from interface:CollectionRemoves all occurrences in thisCollectionof each object in the specifiedCollection(optional). After this method returns none of the elements in the passedCollectioncan be found in thisCollectionanymore.- Specified by:
removeAllin interfaceCollection<K>- Parameters:
c- the collection of objects to remove.- Returns:
trueif thisCollectionis modified,falseotherwise.
-
retainAll
public final boolean retainAll(Collection<?> c)
Description copied from interface:CollectionRemoves all objects from thisCollectionthat are not also found in theCollectionpassed (optional). After this method returns thisCollectionwill only contain elements that also can be found in theCollectionpassed to this method.- Specified by:
retainAllin interfaceCollection<K>- Parameters:
c- the collection of objects to retain.- Returns:
trueif thisCollectionis modified,falseotherwise.
-
-