本文整理汇总了Java中java.util.concurrent.ConcurrentHashMap.forEachEntry方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentHashMap.forEachEntry方法的具体用法?Java ConcurrentHashMap.forEachEntry怎么用?Java ConcurrentHashMap.forEachEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentHashMap
的用法示例。
在下文中一共展示了ConcurrentHashMap.forEachEntry方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testForEachEntrySequentially
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* 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);
}
示例2: testForEachEntryInParallel
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* 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);
}
示例3: testMappedForEachEntrySequentially
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* 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);
}
示例4: testMappedForEachEntryInParallel
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* 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);
}
示例5: main
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public static void main(String[] args) {
ConcurrentHashMap<String, ConcurrentLinkedDeque<Operation>> userHash = new ConcurrentHashMap<>();
HashFiller hashFiller = new HashFiller(userHash);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(hashFiller);
threads[i].start();
}
for (int i = 0; i < 10; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("Size: %d\n", userHash.size());
userHash.forEach(10, (user, list) -> {
System.out.printf("%s: %s: %d\n", Thread.currentThread().getName(), user, list.size());
});
userHash.forEachEntry(10, entry -> {
System.out.printf("%s: %s: %d\n", Thread.currentThread().getName(), entry.getKey(),
entry.getValue().size());
});
Operation op = userHash.search(10, (user, list) -> {
for (Operation operation : list) {
if (operation.getOperation().endsWith("1")) {
return operation;
}
}
return null;
});
System.out.printf("The operation we have found is: %s, %s, %s,\n", op.getUser(), op.getOperation(),
op.getTime());
ConcurrentLinkedDeque<Operation> operations = userHash.search(10, (user, list) -> {
if (list.size() > 10) {
return list;
}
return null;
});
System.out.printf("The user we have found is: %s: %d operations\n", operations.getFirst().getUser(),
operations.size());
int totalSize = userHash.reduce(10, (user, list) -> {
return list.size();
}, (n1, n2) -> {
return n1 + n2;
});
System.out.printf("The total size is: %d\n", totalSize);
}