下面列出了java.util.concurrent.ConcurrentHashMap#reduce ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "Tylor");
map.put(2, "Jenny");
map.put(3, "Tymmy");
map.put(4, "Anna");
map.put(5, "Tysha");
map.put(6, "Maria");
System.out.println("Map: \n" + map);
String result = map.reduce(2, (k, v) -> v = v + " ", (t, v) -> t + v);
System.out.println("\nNames concatenated and separated by tab via reduce():\n" + result);
Integer maxKey = map.reduceKeys(2, (k1, k2) -> k1.compareTo(k2) > 0 ? k1 : k2);
System.out.println("\nMaximum key via reduceKeys(): " + maxKey);
String resultValue = map.reduceValues(2, (v) -> v = v + " ", (t, v) -> t + v);
System.out.println("\nNames concatenated and separated by tab via reduceValues():\n" + resultValue);
}
private static void testReduce() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
String reduced = map.reduce(1, (key, value) -> key + "=" + value,
(s1, s2) -> s1 + ", " + s2);
System.out.println(reduced);
}
private static void testReduce() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
String reduced = map.reduce(1, (key, value) -> key + "=" + value, (s1, s2) -> s1 + ", " + s2);
System.out.println(reduced);
}
private static void testReduce() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
String reduced = map.reduce(1, (key, value) -> key + "=" + value,
(s1, s2) -> s1 + ", " + s2);
System.out.println(reduced);
}
/**
* ConcurrentMap接口继承自Map接口,并定义了最实用的并发集合类型之一。
* Java8通过将新的方法添加到这个接口,引入了函数式编程。
* ConcurrentHashMap 是无序的
*/
public void concurrentMapDemo() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.put("foo", "bar");
map.put("han", "solo");
map.put("r2", "d2");
map.put("c3", "p0");
// 方法可以并行迭代映射中的键值对
map.forEach(1, (k, v) ->
System.out.printf("key: %s; value: %s; thread: %s\n",
k, v, Thread.currentThread().getName()));
System.out.println("\n");
// search 只要返回了非空的结果,就不会往下搜索了
String result = map.search(1, (k, v) -> {
System.out.println(Thread.currentThread().getName());
if ("foo".equals(k)) {
return v;
}
return null;
}
);
System.out.println("Result: " + result);
System.out.println("\n");
String result1 = map.search(1, (k, v) -> {
System.out.println(Thread.currentThread().getName());
if (v.length() > 3) {
return v;
}
return null;
}
);
System.out.println("Result: " + result1);
System.out.println("\n");
String result2 = map.reduce(1,
(k, v) -> {
System.out.println("Transform: " + Thread.currentThread().getName());
return k + "=" + v;
},
(s1, s2) -> {
System.out.println("Reduce: " + Thread.currentThread().getName());
return s1 + ", " + s2;
}
);
System.out.println("Result: " + result2);
}