-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHashMap.java
More file actions
63 lines (51 loc) · 1.16 KB
/
HashMap.java
File metadata and controls
63 lines (51 loc) · 1.16 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
import java.util.ArrayList;
import java.util.List;
public class HashMap {
class Container{
Object key;
Object value;
public void insert(Object k, Object v){
this.key=k;
this.value=v;
}
}
private Container c;
private List<Container> recordList;
public HashMap(){
this.recordList=new ArrayList<Container>();
}
public void put(Object k, Object v){
this.c=new Container();
c.insert(k, v);
//check for the same key before adding
for(int i=0; i<recordList.size(); i++){
Container c1=recordList.get(i);
if(c1.key.equals(k)){
//remove the existing object
recordList.remove(i);
break;
}
}
recordList.add(c);
}
public Object get(Object k){
for(int i=0; i<this.recordList.size(); i++){
Container con = recordList.get(i);
if (k.toString()==con.key.toString()) {
return con.value;
}
}
return null;
}
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("1", "1");
hm.put("2", "2");
hm.put("3", "3");
System.out.println(hm.get("3"));
hm.put("3", "4");
System.out.println(hm.get("1"));
System.out.println(hm.get("3"));
System.out.println(hm.get("8"));
}
}