-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashArray.java
78 lines (70 loc) · 1.59 KB
/
HashArray.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
import java.util.ArrayList;
import java.util.HashMap;
/*
* Created on Feb 20, 2006
* Feb 20, 2006 1:08:42 AM
*/
/*
* provides fast access via key or integer index.
*/
public class HashArray<E>
{
private HashMap<String, Integer> fHash;
private ArrayList<E> fArray;
private int fSize;
public HashArray()
{
fHash = new HashMap<String, Integer>();
fArray = new ArrayList<E>();
fSize = 0;
}
public int add(E data, String key)
{
int loc;
Integer Loc = fHash.get(key);
if (Loc == null)
{
loc = fArray.size();
Loc = new Integer(loc);
fHash.put(key, new Integer(loc));
fArray.add(data);
fSize++;
}
else
{
loc = Loc.intValue();
fArray.set(loc, data);
}
return loc;
}
public E get(int index)
{
return fArray.get(index);
}
public E get(String key)
{
Integer index = fHash.get(key);
if (index != null)
{
return fArray.get(index.intValue());
}
return null;
}
/*
* will still be in the array, but the data will be nulled out.
*/
public void remove(String key)
{
Integer index = fHash.get(key);
if (index != null)
{
fHash.remove(key);
fArray.set(index.intValue(), null);
fSize--;
}
}
public int size()
{
return fSize;
}
}