Java : ConcurrentHashMap.KeySetView with Examples

ConcurrentHashMap.KeySetView (Java SE 20 & JDK 20) API Examples.
You will find code examples on most ConcurrentHashMap.KeySetView methods.


Summary

A view of a ConcurrentHashMap as a 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).

Class diagram

void test(Set<String> set) throws InterruptedException {
    set.add("a");
    set.add("b");
    set.add("c");
    set.add("d");
    set.add("e");

    try (final var executor = Executors.newSingleThreadScheduledExecutor()) {

        executor.schedule(() -> {
            System.out.println("-- add value! --");
            set.add("X");
        }, 5, TimeUnit.SECONDS);

        try {
            for (final var value : set) {
                System.out.println("value = " + value);
                TimeUnit.SECONDS.sleep(2);
            }
        } catch (ConcurrentModificationException e) {
            System.out.println("ConcurrentModificationException!");
        }
    }

    System.out.println("-- end --");
    System.out.println("set = " + set);
}
test(new HashSet<>());

// Result
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//ConcurrentModificationException!
//-- end --
//set = [a, b, c, d, e, X]

test(ConcurrentHashMap.newKeySet());

// Result
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//value = d
//value = e
//value = X
//-- end --
//set = [a, b, c, d, e, X]

Methods

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.

final var set = ConcurrentHashMap.<String>newKeySet();
System.out.println(set); // []

System.out.println(set.add("a")); // true
System.out.println(set); // [a]

System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]

System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]

System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]

boolean addAll (Collection<? extends K> c)

Adds all of the elements in the specified collection to this set, as if by calling add(K) on each one.

final var src = Set.of("a", "b", "c");

{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
    System.out.println(set);  // [a, b, c, d, e, f]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
    System.out.println(set); // [a, b, c, e, f]
}

final void clear ()

Removes all of the elements from this view, by removing all the mappings from the map backing this view.

final var set = ConcurrentHashMap.<String>newKeySet();
System.out.println(set); // []

set.add("a");
System.out.println(set); // [a]

set.add("b");
System.out.println(set); // [a, b]

set.clear();
System.out.println(set); // []

boolean contains (Object o)

Returns true if this collection contains the specified element.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true

System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false

final boolean containsAll (Collection<?> c)

Returns true if this collection contains all of the elements in the specified collection.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.containsAll(Set.of())); // true

System.out.println(set.containsAll(Set.of("a"))); // true
System.out.println(set.containsAll(Set.of("a", "b"))); // true
System.out.println(set.containsAll(Set.of("b", "a"))); // true
System.out.println(set.containsAll(Set.of("c", "b", "a"))); // true

System.out.println(set.containsAll(Set.of("X"))); // false
System.out.println(set.containsAll(Set.of("a", "X"))); // false
System.out.println(set.containsAll(Set.of("a", "b", "c", "X"))); // false

boolean equals (Object o)

Indicates whether some other object is "equal to" this one.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

final var hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true

final var treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true

final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false

ConcurrentHashMap<K,V> getMap ()

Returns the map backing this view.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");

System.out.println(set); // [a, b, c]
System.out.println(set.getMappedValue()); // true

final var map = set.getMap();
System.out.println(map); // {a=true, b=true, c=true}

set.add("X");
System.out.println(set); // [a, b, c, X]
System.out.println(map); // {a=true, b=true, c=true, X=true}

map.remove("b");
System.out.println(set); // [a, c, X]
System.out.println(map); // {a=true, c=true, X=true}

V getMappedValue ()

Returns the default mapped value for additions, or null if additions are not supported.

final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var keys = map.keySet(999);
System.out.println(keys); // [a, b, c]
System.out.println(keys.getMappedValue()); // 999

map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]

keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]

keys.add("X");
System.out.println(map); // {b=2, c=3, d=4, X=999}
System.out.println(keys); // [b, c, d, X]

int hashCode ()

Returns a hash code value for the object.

final var set1 = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set1, "a", "b", "c");
System.out.println(set1.hashCode()); // 294

final var set2 = ConcurrentHashMap.<Integer>newKeySet();
Collections.addAll(set2, 1, 2, 3);
System.out.println(set2.hashCode()); // 6

final boolean isEmpty ()

Returns true if this collection contains no elements.

final var set = ConcurrentHashMap.<String>newKeySet();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false

set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

Iterator<K> iterator ()

Returns an iterator over the elements in this collection.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]

System.out.println("-- iterator --");
set.iterator().forEachRemaining(System.out::println);

// Result
// ↓
//-- iterator --
//aaa
//ccc
//bbb

boolean remove (Object o)

Removes the key from this map view, by removing the key (and its corresponding value) from the backing map.

final var src = Set.of("a", "b", "c");

{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("")); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("a")); // true
    System.out.println(set); // [b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("b")); // true
    System.out.println(set); // [a, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("X")); // false
    System.out.println(set); // [a, b, c]
}

boolean removeAll (Collection<?> c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

final var src = Set.of("a", "b", "c");

{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of())); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a"))); // true
    System.out.println(set); // [b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b"))); // true
    System.out.println(set); // [c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("b", "a"))); // true
    System.out.println(set); // [c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
    System.out.println(set); // []
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("X", "Y"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "X"))); // true
    System.out.println(set); // [b, c]
}

final boolean retainAll (Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection (optional operation).

final var src = Set.of("a", "b", "c");

{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of())); // true
    System.out.println(set); // []
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a"))); // true
    System.out.println(set); // [a]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b"))); // true
    System.out.println(set); // [a, b]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("b", "a"))); // true
    System.out.println(set); // [a, b]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b", "c"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b", "c", "X", "Y"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("X", "a", "b", "Y"))); // true
    System.out.println(set); // [a, b]
}
{
    final var set = ConcurrentHashMap.<String>newKeySet();
    set.addAll(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("X", "Y"))); // true
    System.out.println(set); // []
}

final int size ()

Returns the number of elements in this collection.

final var set1 = ConcurrentHashMap.<String>newKeySet();
System.out.println(set1); // []
System.out.println(set1.size()); // 0

final var set2 = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3

final Object[] toArray ()

Returns an array containing all of the elements in this collection.

final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

final Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]

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 var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

final String[] ret = set.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [a, b, c]
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

{
    final String[] array = new String[3];
    System.out.println(Arrays.toString(array)); // [null, null, null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
    final String[] array = new String[5];
    System.out.println(Arrays.toString(array)); // [null, null, null, null, null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
    System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
    final String[] array = new String[1];
    System.out.println(Arrays.toString(array)); // [null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [null]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}

final String toString ()

Returns a string representation of this collection.

final var set = ConcurrentHashMap.<String>newKeySet();

final var str1 = set.toString();
System.out.println(str1); // []

set.add("a");

final var str2 = set.toString();
System.out.println(str2); // [a]

set.add("b");

final var str3 = set.toString();
System.out.println(str3); // [a, b]

set.add("c");

final var str4 = set.toString();
System.out.println(str4); // [a, b, c]

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Set

clear, containsAll, isEmpty, removeAll, retainAll, size, spliterator, toArray, toArray

Please see the link below.


Related posts

To top of page