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

The following examples show how to use java.util.concurrent.ConcurrentHashMap#forEachValue() . 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 check out the related API usage on the sidebar.
Example 1
Source File: Concurrency1.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 9 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 2
Source File: Concurrency1.java    From java8-tutorial with 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 File: Main.java    From Java-Coding-Problems with 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
Source File: ConcurrentHashMap8Test.java    From openjdk-jdk9 with GNU General Public License v2.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 5
Source File: ConcurrentHashMap8Test.java    From openjdk-jdk9 with GNU General Public License v2.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));
}
 
Example 6
Source File: ConcurrentHashMap8Test.java    From j2objc with 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 File: ConcurrentHashMap8Test.java    From j2objc with 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));
}