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);
}
}