Java Code Examples for java.util.concurrent.ConcurrentHashMap#reduce()

The following examples show how to use java.util.concurrent.ConcurrentHashMap#reduce() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Java-Coding-Problems   File: Main.java    License: MIT License 6 votes vote down vote up
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);        
    }
 
Example 2
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);
}
 
Example 3
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);
}
 
Example 4
Source Project: java8-tutorial   File: ConcurrentHashMap1.java    License: MIT License 5 votes vote down vote up
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);
}
 
Example 5
Source Project: java-tutorial   File: ConcurrentMapTest.java    License: MIT License 4 votes vote down vote up
/**
 * 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);
}