java 交集测试代码

package com.example.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

public class TestRetainSet {
    private static final AtomicLong counterMap = new AtomicLong();
    private static final AtomicLong counterContains = new AtomicLong();
    private static final AtomicLong counterRetainAll = new AtomicLong();
    private static final AtomicLong counterMap2 = new AtomicLong();
    private static final AtomicLong counterContains2 = new AtomicLong();
    private static final Set<String> KEY_SET1 = ConcurrentHashMap.newKeySet();
    private static final Set<String> KEY_SET2 = ConcurrentHashMap.newKeySet();

    private static final int execCount = 10;

    static {
        int loopCount = 10000;

        // 初始化数据
        for (int i = 0; i < loopCount; i++) {
            String uuid = UUID.randomUUID()
                .toString();
            KEY_SET1.add("test1" + uuid);
        }
        for (int i = 0; i < loopCount; i++) {
            String uuid = UUID.randomUUID()
                .toString();
            KEY_SET2.add("test2" + uuid);
        }
        KEY_SET1.addAll(KEY_SET2);
        // KEY_SET2.addAll(KEY_SET1);
        System.out.println("添加完成");
        for (int i = 0; i < execCount; i++) {
            init();
            init2();
            init3();
        }
        long total1 = counterMap.longValue();
        long total2 = counterContains.longValue();
        long total3 = counterRetainAll.longValue();
        long avg1 = counterMap.longValue() / execCount;
        long avg2 = counterContains.longValue() / execCount;
        long avg3 = counterRetainAll.longValue() / execCount;
        counterMap.set(0L);
        counterContains.set(0L);
        counterRetainAll.set(0L);
        for (int i = 0; i < execCount; i++) {
            init();
            init2();
            init3();
        }

        System.out.println("total1 = " + total1);
        System.out.println("total2 = " + total2);
        System.out.println("total3 = " + total3);
        System.out.println("avg1 = " + avg1);
        System.out.println("avg2 = " + avg2);
        System.out.println("avg3 = " + avg3);
    }

    public static void init() {
        testRetainAll();
        testContains();
        testContains2();
        testRetainAll();
        testMap();
        testMap2();
        System.out.println("------------------------");
    }

    public static void init2() {
        testContains();
        testContains2();
        testRetainAll();
        testMap();
        testMap2();
        System.out.println("------------------------");
    }

    public static void init3() {
        testContains();
        testContains2();
        testMap();
        testMap2();
        testRetainAll();
        System.out.println("------------------------");
    }

    public static void main(String[] args) {
        System.out.println("#######################################");
        System.out.println("lambda.counterMap  total= " + counterMap.longValue());
        System.out.println("for.counterMap2  total= " + counterMap2.longValue());
        System.out.println("lambda.counterContains  total= " + counterContains.longValue());
        System.out.println("for.counterContains2  total= " + counterContains2.longValue());
        System.out.println("counterRetainAll  total= " + counterRetainAll.longValue());
        System.out.println("------------------------");
        System.out.println("lambda.counterMap avg = " + counterMap.longValue() / execCount);
        System.out.println("for.counterMap2 avg = " + counterMap2.longValue() / execCount);
        System.out.println("lambda.counterContains avg = " + counterContains.longValue() / execCount);
        System.out.println("for.counterContains2 avg = " + counterContains2.longValue() / execCount);
        System.out.println("counterRetainAll avg = " + counterRetainAll.longValue() / execCount);

    }

    public static void testMap() {

        long startTime = System.currentTimeMillis();
        Set<String> r = ConcurrentHashMap.newKeySet();
        Map<String, String> map1 = KEY_SET1.stream()
            .collect(toMap(s -> s, Function.<String>identity()));
        KEY_SET2.stream()
            .forEach(s -> {
                String ss = map1.get(s);
                if (ss != null) {
                    r.add(ss);
                }
            });
        long execTime = System.currentTimeMillis() - startTime;
        System.out.print(r.size());
        System.out.println("testMap:" + execTime);
        counterMap.addAndGet(execTime);
    }

    public static void testMap2() {

        long startTime = System.currentTimeMillis();
        Set<String> r = ConcurrentHashMap.newKeySet();
        Map<String, String> map1 = new HashMap<>();
        for (String s1 : KEY_SET1) {
            if (map1.put(s1, s1) != null) {
                throw new IllegalStateException("Duplicate key");
            }
        }
        for (String s : KEY_SET2) {
            String ss = map1.get(s);
            if (ss != null) {
                r.add(ss);
            }
        }
        long execTime = System.currentTimeMillis() - startTime;
        System.out.print(r.size());
        System.out.println("testMap:" + execTime);
        counterMap2.addAndGet(execTime);
    }

    public static void testRetainAll() {
        Set<String> set = ConcurrentHashMap.newKeySet();
        set.addAll(KEY_SET1);
        long startTime = System.currentTimeMillis();

        set.retainAll(KEY_SET2);
        long execTime = System.currentTimeMillis() - startTime;
        System.out.print(set.size());
        System.out.println("testRetainAll:" + execTime);
        counterRetainAll.addAndGet(execTime);
    }

    /**
     * 40
     */
    public static void testContains() {

        long startTime = System.currentTimeMillis();
        Set<String> r = ConcurrentHashMap.newKeySet();
        KEY_SET1.stream()
            .forEach(s -> {
                if (KEY_SET2.contains(s)) {
                    r.add(s);
                }
            });
        long execTime = System.currentTimeMillis() - startTime;
        System.out.print(r.size());
        System.out.println("testContains:" + execTime);
        counterContains.addAndGet(execTime);
    }

    public static void testContains2() {

        long startTime = System.currentTimeMillis();
        Set<String> r = ConcurrentHashMap.newKeySet();
        for (String s : KEY_SET1) {
            if (KEY_SET2.contains(s)) {
                r.add(s);
            }
        }
        long execTime = System.currentTimeMillis() - startTime;
        System.out.print(r.size());
        System.out.println("testContains:" + execTime);
        counterContains2.addAndGet(execTime);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值