-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashsetTest.java
More file actions
35 lines (32 loc) · 1.18 KB
/
Copy pathhashsetTest.java
File metadata and controls
35 lines (32 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Algorithm;
import java.util.HashSet;
import java.util.Random;
public class hashsetTest {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
for (int i = 0; i < 2500000; i++) {
set.add(getRandomString(25));
}
long startTime = System.currentTimeMillis();
for (int i = 0; i < 2500000; i++) {
String getRandomString = getRandomString(5) +
getRandomString(5) +
getRandomString(5) +
getRandomString(5) +
getRandomString(5);
set.contains(getRandomString);
}
long endTime = System.currentTimeMillis();
System.out.println("拼接+过滤用时:" + (endTime - startTime) / 1000 + "s");
}
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
}