-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.java
173 lines (173 loc) · 5.24 KB
/
HashMap.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.*;
public class HashMap<K,V>{
public class Node{
K key;
V value;
Node next;
public Node(K key,V val){
this.key=key;
this.value=val;
}
}
ArrayList<Node> table;
int size=0;
public HashMap(int size){
this.table=new ArrayList<>(10);
for(int i=0;i<size;i++){
table.add(null);
}
}
public HashMap(){
this(4);
}
public void put(K key,V val){
int idx=generateIndex(key);
Node temp=table.get(idx);
Node prev=temp;
if(temp!=null){
if(temp.key.equals(key)){
temp.value=val;
return ;
}
temp=temp.next;
}
Node nn=new Node(key, val);
table.set(idx,nn);
nn.next=prev;
size++;
int lf=size/table.size();
int thf=2;
if(lf>thf){
rehashing();
}
}
public void rehashing(){
ArrayList<Node> li=new ArrayList<>();
for(int i=0;i<2*table.size();i++){
li.add(null);
}
ArrayList<Node> list=this.table;
this.table=li;
for(Node node:list){
while(node!=null){
table.add(node);
node=node.next;
}
}
}
public V get(K key){
int idx=generateIndex(key);
Node temp=table.get(idx);
while(temp!=null){
if(temp.key.equals(key)){
return temp.value;
}
temp=temp.next;
}
return null;
}
public void display(){
for(Node temp:table){
while(temp!=null){
System.out.println(temp.key+" "+temp.value);
temp=temp.next;
}
}
}
public void remove(K key){
int idx=generateIndex(key);
Node temp=table.get(idx);
if(temp==null){
return ;
}
if(temp.key.equals(key)){
table.set(idx,temp.next);
size--;
return;
}
Node prev=null;
while(temp!=null){
if(temp.key.equals(key)){
prev.next=temp.next;
size--;
return;
}
prev=temp;
temp=temp.next;
}
}
public boolean containsKey(K key){
int idx=generateIndex(key);
Node temp=table.get(idx);
while (temp!=null) {
if(temp.key.equals(key)){
return true;
}
temp=temp.next;
}
return false;
}
public int generateIndex(K key){
return key.hashCode()%table.size();
}
public static void main(String args[]){
HashMap<Integer,Integer> map=new HashMap<>(20);
map.put(1,10);
map.put(2,20);
map.put(2,20);
map.put(3,30);
map.put(4,40);
map.put(5,50);
map.put(6,60);
map.put(7,70);
map.put(8,80);
map.put(9,90);
map.put(10,100);
map.put(11,110);
map.put(12,120);
map.put(13,130);
map.put(14,140);
map.put(15,150);
map.put(16,160);
map.put(17,170);
map.put(18,180);
map.put(19,190);
map.put(20,200);
map.display();
System.out.println();
System.out.println(map.get(1));
System.out.println(map.get(2));
System.out.println(map.get(3));
System.out.println(map.get(4));
System.out.println(map.get(5));
System.out.println(map.get(6));
System.out.println(map.get(7));
System.out.println(map.get(8));
System.out.println(map.get(9));
System.out.println(map.get(10));
map.remove(6);
map.remove(7);
map.remove(8);
map.remove(9);
map.remove(10);
map.display();
System.out.println(map.get(1));
System.out.println(map.get(2));
System.out.println(map.get(3));
System.out.println(map.get(4));
System.out.println(map.get(5));
System.out.println(map.containsKey(1));
System.out.println(map.containsKey(2));
System.out.println(map.containsKey(3));
System.out.println(map.containsKey(4));
System.out.println(map.containsKey(5));
System.out.println(map.containsKey(6));
System.out.println(map.containsKey(7));
System.out.println(map.containsKey(8));
System.out.println(map.containsKey(9));
System.out.println(map.containsKey(10));
System.out.println();
map.display();
System.out.println("size "+map.size);
}
}