Java Code Examples for java.util.concurrent.ConcurrentHashMap#forEachValue()

The following examples show how to use java.util.concurrent.ConcurrentHashMap#forEachValue() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
public static void main(String[] args) {
    ConcurrentHashMap<Integer, UUID> concurrentHashMap = new ConcurrentHashMap<>();

    for (int i = 0; i < 100; i++) {
        concurrentHashMap.put(i, UUID.randomUUID());
    }

    int threshold = 1;

    concurrentHashMap.forEachValue(threshold, System.out::println);

    concurrentHashMap.forEach((id, uuid) -> {
        if (id % 10 == 0) {
            System.out.println(String.format("%s: %s", id, uuid));
        }
    });

    UUID searchResult = concurrentHashMap.search(threshold, (id, uuid) -> {
        if (String.valueOf(uuid).startsWith(String.valueOf(id))) {
            return uuid;
        }
        return null;
    });

    System.out.println(searchResult);
}
 
Example 2
Source Project: java8-tutorial   File: Concurrency1.java    License: MIT License 6 votes vote down vote up
public static void main(String[] args) {
    ConcurrentHashMap<Integer, UUID> concurrentHashMap = new ConcurrentHashMap<>();

    for (int i = 0; i < 100; i++) {
        concurrentHashMap.put(i, UUID.randomUUID());
    }

    int threshold = 1;

    concurrentHashMap.forEachValue(threshold, System.out::println);

    concurrentHashMap.forEach((id, uuid) -> {
        if (id % 10 == 0) {
            System.out.println(String.format("%s: %s", id, uuid));
        }
    });

    UUID searchResult = concurrentHashMap.search(threshold, (id, uuid) -> {
        if (String.valueOf(uuid).startsWith(String.valueOf(id))) {
            return uuid;
        }
        return null;
    });

    System.out.println(searchResult);
}
 
Example 3
Source Project: Java-Coding-Problems   File: Main.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {

        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        map.put(3, 1);
        map.put(4, 1);
        map.put(5, 1);
        map.put(6, 1);

        System.out.println("Map: \n" + map);

        System.out.println("\nMap displayed via forEach():");
        map.forEach(2, (k, v) -> System.out.println("key: " + k + ", Value: " + v));

        System.out.println("\nMap displayed via forEach() after incrementing each value with 1:");
        map.forEach(2, (k, v) -> v = v + 1, v -> System.out.println("Value: " + v));

        System.out.println("\nMap displayed via forEachEntry():");
        map.forEachEntry(2, (e) -> System.out.println("key: " + e.getKey() + ", Value: " + e.getValue()));

        System.out.println("\nCheck if key=value via forEachEntry():");
        map.forEachEntry(2, (e) -> Objects.equals(e.getKey(), e.getValue()), System.out::println);

        System.out.println("\nDisplay keys via forEachKey():");
        map.forEachKey(2, System.out::println);

        System.out.println("\nKeys displayed via forEachKey() after incrementing each key with 1:");
        map.forEachKey(2, (k) -> k = k + 1, System.out::println);

        System.out.println("\nDisplay values via forEachValue():");
        map.forEachValue(2, System.out::println);

        System.out.println("\nKeys displayed via forEachValue() after incrementing each value with 1:");
        map.forEachValue(2, (v) -> v = v + 1, System.out::println);
    }
 
Example 4
/**
 * forEachValueSequentially traverses all values
 */
public void testForEachValueSequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), SIZE * (SIZE - 1));
}
 
Example 5
/**
 * forEachValueInParallel traverses all values
 */
public void testForEachValueInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), SIZE * (SIZE - 1));
}
 
Example 6
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * forEachValueSequentially traverses all values
 */
public void testForEachValueSequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), SIZE * (SIZE - 1));
}
 
Example 7
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * forEachValueInParallel traverses all values
 */
public void testForEachValueInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), SIZE * (SIZE - 1));
}