| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- public class HTMap<K,V>{
- private class Entry<K,V> {
- K key;
- V value;
- @Override
- public int hashCode(){
- return key.hashCode();
- }
- }
- private Entry<K,V>[] table;
- private int size;
- public HTMap(int size){
- this.size = size;
- table = (Entry<K,V>[]) new HTMap.Entry[size];
- }
- public V put(K key, V value){
- int i = Math.abs(key.hashCode() % table.length);
- V old = null;
- while (table[i] != null && !table[i].key.equals(key)) {
- i = (i + 1) % table.length;
- }
- if(table[i].key == key){
- old = table[i].value;
- }
- table[i].key = key;
- table[i].value = value;
- return old;
- }
- public V get(K key){
- int i = Math.abs(key.hashCode() % table.length);
- while (table[i] != null && !table[i].key.equals(key)) {
- i = (i + 1) % table.length;
- }
- return table[i] != null ? table[i].value : null;
- }
- }
|