-
Notifications
You must be signed in to change notification settings - Fork 65
/
AssociativeArray.java
173 lines (153 loc) · 3.34 KB
/
AssociativeArray.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
/**
* Data-Structures-In-Java
* AssociativeArray.java
*/
package com.deepak.data.structures.Arrays;
/**
* Associative Arrays in Java :
*
* Associative arrays are data structure, where keys are associated with some sort
* of values. Though java does not support associative arrays, but same functionality
* can be achieved through HashMap or HashTable.
* Below is the replica of HashMap just names as associative arrays.
*
* @author Deepak
*/
public class AssociativeArray<K, V> {
/* Table to hold key value pairs */
private Entry<K, V>[] table;
/* Variable to hold size of the map */
private int size;
/* Default table size */
private static final int DEFAULT_TABLE_SIZE = 10;
/**
* Constructor
*/
public AssociativeArray() {
this(DEFAULT_TABLE_SIZE);
}
/**
* Constructor
*
* @param size
*/
@SuppressWarnings("unchecked")
public AssociativeArray(int size) {
this.table = new Entry[size];
this.size = 0;
}
/**
* Method to add/update a entry
*
* @param key
* @param value
*/
public void put(K key, V value) {
boolean isNewEntry = true;
/* Find the hash of the key and bucket it belongs to */
int hash = key.hashCode();
int bucket = getBucket(hash);
Entry<K, V> entry = table[bucket];
/* Loop through the entry of the associative array
* and check if it's just a value update or a new key, value */
while (entry != null) {
if (entry.getHash() == hash && entry.getKey().equals(key)) {
isNewEntry = false;
entry.value = value;
}
entry = entry.next;
}
/* Create a new entry and push to array */
if (isNewEntry) {
Entry<K, V> newEntry = new Entry<>(key, value, hash);
newEntry.next = table[bucket];
table[bucket] = newEntry;
size++;
}
}
/**
* Method to find the value for a given key
*
* @param key
* @return {@link V}
*/
public V get(K key) {
/* Find hash code and bucket associated with key */
int hash = key.hashCode();
int bucket = getBucket(hash);
/* Get all the entries on that column of bucket */
Entry<K, V> entry = table[bucket];
while (entry != null) {
/* If hash and key matches, return the value */
if (entry.getHash() == hash && entry.getKey() == key) {
return entry.getValue();
}
entry = entry.next;
}
return null;
}
/**
* Method to get size of associative array
*
* @return {@link int}
*/
public int size() {
return size;
}
/**
* Method to check if associative array is empty
*
* @return {@link boolean}
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to get bucket based on the hash
*
* @param hash
* @return {@link int}
*/
private int getBucket(int hash) {
return (hash % table.length);
}
/**
* Static class Entry for associative array
*
* @author Deepak
*
* @param <K>
* @param <V>
*/
public static class Entry<K, V> {
/* Variables for key, value, hash and next entry */
private final K key;
private V value;
private Entry<K, V> next;
private int hash;
/**
* Constructor
*
* @param key
* @param value
* @param hash
*/
public Entry(K key, V value, int hash) {
this.key = key;
this.value = value;
this.hash = hash;
}
public Entry<K, V> getNext() {
return next;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public int getHash() {
return hash;
}
}
}