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

The following examples show how to use java.util.concurrent.ConcurrentHashMap#forEachEntry() . 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 check out the related API usage on the sidebar.
Example 1
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 2
/**
 * forEachEntrySequentially traverses all entries
 */
public void testForEachEntrySequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 3
/**
 * forEachEntryInParallel traverses all entries
 */
public void testForEachEntryInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 4
/**
 * Mapped forEachEntrySequentially traverses the given
 * transformations of all entries
 */
public void testMappedForEachEntrySequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
                               (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 5
/**
 * Mapped forEachEntryInParallel traverses the given
 * transformations of all entries
 */
public void testMappedForEachEntryInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
                             (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 6
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * forEachEntrySequentially traverses all entries
 */
public void testForEachEntrySequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 7
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * forEachEntryInParallel traverses all entries
 */
public void testForEachEntryInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 8
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Mapped forEachEntrySequentially traverses the given
 * transformations of all entries
 */
public void testMappedForEachEntrySequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
                               (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
 
Example 9
Source Project: j2objc   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Mapped forEachEntryInParallel traverses the given
 * transformations of all entries
 */
public void testMappedForEachEntryInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
                             (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}