下面列出了java.util.concurrent.ConcurrentHashMap#search ( ) 实例代码,或者点击链接到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);
Integer key = map.search(2, (k, v) -> v.length() < 5 ? k : null);
System.out.println("\nFind key via search(): " + key);
Map.Entry<Integer, String> entry
= map.searchEntries(2, (e) -> e.getValue().startsWith("A") ? e : null);
System.out.println("\nFind entry via searchEntries(): " + entry);
Integer keyFound = map.searchKeys(2, (k) -> k > 4 ? k : null);
System.out.println("\nFind key via searchKeys(): " + keyFound);
String valueFound = map.searchValues(2, (v) -> v.startsWith("M") ? v : null);
System.out.println("\nFind value via searchValues(): " + valueFound);
}
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);
}
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);
}
private static void testSearch() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
System.out.println("\nsearch()\n");
String result1 = map.search(1, (key, value) -> {
System.out.println(Thread.currentThread().getName());
if (key.equals("foo") && value.equals("bar")) {
return "foobar";
}
return null;
});
System.out.println(result1);
System.out.println("\nsearchValues()\n");
String result2 = map.searchValues(1, value -> {
System.out.println(Thread.currentThread().getName());
if (value.length() > 3) {
return value;
}
return null;
});
System.out.println(result2);
}
private static void testSearch() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
System.out.println("\nsearch()\n");
String result1 = map.search(1, (key, value) -> {
System.out.println(Thread.currentThread().getName());
if ("foo".equals(key) && "bar".equals(value)) {
return "foobar";
}
return null;
});
System.out.println(result1);
System.out.println("\nsearchValues()\n");
String result2 = map.searchValues(1, value -> {
System.out.println(Thread.currentThread().getName());
if (value.length() > 3) {
return value;
}
return null;
});
System.out.println(result2);
}
private static void testSearch() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
System.out.println("\nsearch()\n");
String result1 = map.search(1, (key, value) -> {
System.out.println(Thread.currentThread().getName());
if (key.equals("foo") && value.equals("bar")) {
return "foobar";
}
return null;
});
System.out.println(result1);
System.out.println("\nsearchValues()\n");
String result2 = map.searchValues(1, value -> {
System.out.println(Thread.currentThread().getName());
if (value.length() > 3) {
return value;
}
return null;
});
System.out.println(result2);
}
/**
* 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);
}