99 *
1010 * @author Justin Wetherell <phishman3579@gmail.com>
1111 */
12- public class HashMap {
12+ public class HashMap < T extends Number > {
1313
1414 private int hashingKey = 10 ;
15- private List <Integer >[] map = null ;
15+ private List <T >[] map = null ;
1616 private int size = 0 ;
1717
1818 public HashMap () {
1919 initializeMap ();
2020 }
2121
22- public HashMap (Integer [] values ) {
22+ public HashMap (T [] values ) {
2323 hashingKey = values .length ;
2424 if (hashingKey >100 ) hashingKey = 100 ;
2525 initializeMap ();
@@ -30,48 +30,48 @@ public HashMap(Integer[] values) {
3030 private void initializeMap () {
3131 map = new ArrayList [hashingKey ];
3232 for (int i =0 ; i <map .length ; i ++) {
33- map [i ] = new ArrayList <Integer >();
33+ map [i ] = new ArrayList <T >();
3434 }
3535 }
3636
37- private void populate (Integer [] values ) {
38- for (int v : values ) {
37+ private void populate (T [] values ) {
38+ for (T v : values ) {
3939 put (v ,v );
4040 }
4141 }
4242
43- private int hashingFunction (int key ) {
44- return key % hashingKey ;
43+ private int hashingFunction (T key ) {
44+ return key . intValue () % hashingKey ;
4545 }
4646
47- public boolean put (int key , int value ) {
47+ public boolean put (T key , T value ) {
4848 int hashedKey = hashingFunction (key );
49- List <Integer > list = map [hashedKey ];
49+ List <T > list = map [hashedKey ];
5050 // Do not add duplicates
5151 for (int i =0 ; i <list .size (); i ++) {
52- int v = list .get (i );
52+ T v = list .get (i );
5353 if (v == value ) return false ;
5454 }
5555 list .add (value );
5656 size ++;
5757 return true ;
5858 }
5959
60- public boolean remove (int key ) {
60+ public boolean remove (T key ) {
6161 int hashedKey = hashingFunction (key );
62- List <Integer > list = map [hashedKey ];
63- if (list .remove (( Object ) key )) {
62+ List <T > list = map [hashedKey ];
63+ if (list .remove (key )) {
6464 size --;
6565 return true ;
6666 }
6767 return false ;
6868 }
6969
70- public boolean contains (int value ) {
70+ public boolean contains (T value ) {
7171 for (int key =0 ; key <map .length ; key ++) {
72- List <Integer > list = map [key ];
72+ List <T > list = map [key ];
7373 for (int item =0 ; item <list .size (); item ++) {
74- int v = list .get (item );
74+ T v = list .get (item );
7575 if (v == value ) return true ;
7676 }
7777 }
@@ -89,10 +89,10 @@ public int getSize() {
8989 public String toString () {
9090 StringBuilder builder = new StringBuilder ();
9191 for (int key =0 ; key <map .length ; key ++) {
92- List <Integer > list = map [key ];
92+ List <T > list = map [key ];
9393 for (int item =0 ; item <list .size (); item ++) {
94- int value = list .get (item );
95- if (value !=Integer . MIN_VALUE ) builder .append (key ).append ("=" ).append (value ).append (", " );
94+ T value = list .get (item );
95+ if (value !=null ) builder .append (key ).append ("=" ).append (value ).append (", " );
9696 }
9797 }
9898 return builder .toString ();
0 commit comments