22
33public class HashMap {
44
5- private int hsize ;
6- private LinkedList [] buckets ;
5+ private final int hsize ;
6+ private final LinkedList [] buckets ;
77
88 public HashMap (int hsize ) {
99 buckets = new LinkedList [hsize ];
1010 for (int i = 0 ; i < hsize ; i ++) {
1111 buckets [i ] = new LinkedList ();
12- // Java requires explicit initialisaton of each object
12+ // Java requires explicit initialization of each object
1313 }
1414 this .hsize = hsize ;
1515 }
@@ -59,31 +59,22 @@ public void insert(int key) {
5959 }
6060
6161 private Node findEnd (Node n ) {
62- if (n .getNext () == null ) {
63- return n ;
64- } else {
65- return findEnd (n .getNext ());
62+ while (n .getNext () != null ) {
63+ n = n .getNext ();
6664 }
65+ return n ;
6766 }
6867
6968 public Node findKey (int key ) {
7069 if (!isEmpty ()) {
71- return findKey (first , key );
72- } else {
73- System .out .println ("List is empty" );
74- return null ;
75- }
76- }
70+ Node temp = first ;
71+ if (temp .getKey () == key ) return temp ;
7772
78- private Node findKey (Node n , int key ) {
79- if (n .getKey () == key ) {
80- return n ;
81- } else if (n .getNext () == null ) {
82- System .out .println ("Key not found" );
83- return null ;
84- } else {
85- return findKey (n .getNext (), key );
73+ while ((temp = temp .getNext ()) != null ) {
74+ if (temp .getKey () == key ) return temp ;
75+ }
8676 }
77+ return null ;
8778 }
8879
8980 public void delete (int key ) {
@@ -95,8 +86,6 @@ public void delete(int key) {
9586 } else {
9687 delete (first , key );
9788 }
98- } else {
99- System .out .println ("List is empty" );
10089 }
10190 }
10291
@@ -132,7 +121,7 @@ public boolean isEmpty() {
132121 public static class Node {
133122
134123 private Node next ;
135- private int key ;
124+ private final int key ;
136125
137126 public Node (int key ) {
138127 next = null ;
0 commit comments