Java Code Examples for java.util.concurrent.ConcurrentHashMap#search()
The following examples show how to use
java.util.concurrent.ConcurrentHashMap#search() .
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 File: Concurrency1.java From javacore with Creative Commons Attribution Share Alike 4.0 International | 9 votes |
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); }
Example 2
Source File: Main.java From Java-Coding-Problems with MIT License | 6 votes |
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); }
Example 3
Source File: Concurrency1.java From java8-tutorial with MIT License | 6 votes |
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); }
Example 4
Source File: ConcurrentMapTest.java From java-tutorial with MIT License | 5 votes |
/** * 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); }
Example 5
Source File: ProgMainExecServConcurrentHashMap09.java From javase with MIT License | 5 votes |
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); }
Example 6
Source File: ConcurrentHashMap1.java From javacore with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
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); }
Example 7
Source File: ConcurrentHashMap1.java From java8-tutorial with MIT License | 5 votes |
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); }