11package HashTable .LinearProbing ;
22
33import HashTable .HashTableInterface ;
4- import java .util .Random ;
54import java .util .function .Function ;
65
7- public class HashTable <K , V > implements HashTableInterface <K , V > {
6+ public class HashTableLP <K , V > implements HashTableInterface <K , V > {
87
98 private Node [] array ;
109 private Function <K , Integer > foh ;
@@ -13,12 +12,12 @@ public class HashTable<K, V> implements HashTableInterface<K, V> {
1312 private static int numCollisions = 0 ;
1413 private static int numResizes = 0 ;
1514
16- public HashTable () {
15+ public HashTableLP () {
1716 array = new Node [INITIAL_TABLE_SIZE ];
1817 foh = getHashingFunction ();
1918 }
2019
21- public HashTable (Function <K , Integer > f ) {
20+ public HashTableLP (Function <K , Integer > f ) {
2221 array = new Node [INITIAL_TABLE_SIZE ];
2322 foh = f ;
2423 }
@@ -80,8 +79,7 @@ public void resize() {
8079 numResizes ++;
8180 clear ();
8281
83- for (int i = 0 ; i < oldArray .length ; i ++) {
84- Node <K , V > node = oldArray [i ];
82+ for (Node <K , V > node : oldArray ) {
8583 if (node != null ) {
8684 put (node .getKey (), node .getValue ());
8785 }
@@ -132,7 +130,7 @@ public void printGraph() {
132130
133131 public static void main (String [] args ) {
134132
135- HashTable <String , String > ht = new HashTable <>();
133+ HashTableLP <String , String > ht = new HashTableLP <>();
136134 ht .put ("banana" , "yellow" );
137135 ht .put ("apple" , "green" );
138136 ht .put ("android" , "green" );
@@ -151,13 +149,13 @@ public static void main(String[] args) {
151149 String deletedKey = "apple" ;
152150 System .out .println (deletedKey + " : i -> " + ht .indexOf (deletedKey ) + " -> " + ht .get (deletedKey ));
153151
154- System .out .println ("----------- Delete -----------" );
152+ System .out .println ("----------- Delete " + deletedKey + " -----------" );
155153 ht .delete (deletedKey );
156154 ht .printGraph ();
157155 ht .printStatus ();
158156
159157 // Testing
160- HashTable <String , String > table = new HashTable <>();
158+ HashTableLP <String , String > table = new HashTableLP <>();
161159 for (int i = 0 ; i < 100000 ; i ++) {
162160 table .put (HashTableInterface .randomString (), HashTableInterface .randomString ());
163161 }
0 commit comments