下面列出了java.util.concurrent.ConcurrentHashMap#forEachEntry ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}