-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
159 lines (126 loc) · 3.05 KB
/
HashMap.java
File metadata and controls
159 lines (126 loc) · 3.05 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.io.*;
import java.util.Random;
public class HashMap {
static int SIZE = 100000;
static int hashindex =0;
static int size = 0;
static Node[] head = new Node[SIZE+10];
static Node[] hashpool = new Node[SIZE+10];
private static class Node {
private Node next,prev;
private int key;
private char[] data;
Node(){
}
}
public static Node getNewNode(){
hashindex++;
return hashpool[hashindex-1];
}
public static int getKey(int val){
return val%(SIZE+10);
}
static void insert(int key, Node newnode){
Node temp = head[key];
if(head[key].next == null){
newnode.prev = head[key];
newnode.next = null;
head[key].next = newnode;
newnode.key =1;
}else{
newnode.prev = head[key];
newnode.next = temp.next;
temp.next.prev = newnode;
newnode.key++;
}
}
static Node search(int hashkey, Node newnode){
Node node = head[hashkey];
while(node.next!= null){
if(isSame(node.next.data, newnode.data)){
return node.next;
}
node = node.next;
}
return null;
}
public static boolean isSame(char []stra, char []strb){
for(int i=0;i<strb.length;i++)
if(stra[i] != strb[i])
return false;
return true;
}
public static String toString(char []data){
StringBuilder sb= new StringBuilder();
sb.append(data);
return sb.toString();
}
static int getchar(char strtemp[]){
int val = 0;
for (int i =0 ;i< 5 ;i++){
val += val *26 +strtemp[i]-'a';
val%=SIZE;
}
return val;
}
public static void main(String[] args) throws IOException {
PrintStream mytxt=new PrintStream("./log.txt");
PrintStream out=System.out;
System.setOut(mytxt);
//HashMap hash = new HashMap();
for(int i=0;i<head.length;i++){
head[i] = new Node();
hashpool[i] = new Node();
}
Random ramdon = new Random();
int max = 1;
int key =0;
System.out.println("start");
char[] str1 = new char[500];
for(int i = 0; i < 100000; i++){
for(int j = 0; j < 500; j++){
str1[j] = (char) (ramdon.nextInt(26) + 'a');
}
int temp = getchar(str1);
int hashkey = getKey(temp);
Node newnode = getNewNode();
newnode.data = str1;
System.out.println("insert string: "+toString(newnode.data) + " ");
Node sernode = search(hashkey,newnode);
int num =1;
if(sernode == null){
insert(hashkey,newnode);
}else{
num = sernode.key++;
}
if(num>max){
max=num;
}
}
System.out.println(max);
System.setOut(out);
System.out.println("ÈÕÆÚ±£´æÍê±Ï¡£");
//in.nextToken();
// for(int i=0;i<10;i++){
// Node newnode = getNewNode();
// newnode.data = i+1;
// int hashkey = getKey(i);
//
// insert(hashkey,newnode);
// Node newnode1 = getNewNode();
// newnode1.data = i+2;
// insert(hashkey,newnode1);
//
// Node newnode2 = getNewNode();
// newnode2.data = i+3;
// insert(hashkey,newnode2);
//
// Node newnode4 = getNewNode();
// newnode4.data = 2;
//
// Node node = search(newnode4);
// if(node != null)
// System.out.print(node.data + " ");
// }
}
}