-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMap.java
54 lines (47 loc) · 794 Bytes
/
MyMap.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
import java.util.*;
public class MyMap<T1, T2>
{
LinkedList<MapElement<T1,T2>> vals;
int size;
public MyMap()
{
vals = new LinkedList<MapElement<T1,T2>>();
size = 0;
}
public boolean put(T1 key, T2 value)
{
MapElement<T1,T2> el = new MapElement<T1,T2>(key, value);
if(!contain(el.getKey()))
{
vals.add(el);
size++;
return true;
}
else
return false;
}
public boolean contain(T1 key)
{
for(int i = 0; i < size; i++)
{
MapElement<T1, T2> temp = vals.get(i);
if(key.equals(temp.getKey()))
{
return true;
}
}
return false;
}
public T2 get(T1 key)
{
for(int i = 0; i < size; i++)
{
MapElement<T1, T2> temp = vals.get(i);
if(key.equals(temp.getKey()))
{
return temp.getVal();
}
}
return null;
}
}