Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package map;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6.  
  7. public class SimpleHashMap<K,V> implements Map<K,V> {
  8.     int size;
  9.     int Capacity;
  10.     private Entry<K,V>[] E;
  11.    
  12.     public SimpleHashMap(){
  13.         E = (Entry<K,V>[]) new Entry[16];
  14.         Capacity = 16;
  15.         size = 0;
  16.     }
  17.     public SimpleHashMap(int Capacity){
  18.         E = (Entry<K,V>[]) new Entry[Capacity];
  19.         size = 0;
  20.         this.Capacity=Capacity;
  21.     }
  22.    
  23.     @Override
  24.     public V get(K key) {
  25.         int index = index(key);
  26.         if(index == -1) {
  27.             return null;
  28.         } else {
  29.             return find(index(key),key).value;
  30.         }
  31.     }
  32.  
  33.     @Override
  34.     public boolean isEmpty() {
  35.         return size == 0;
  36.     }
  37.  
  38.     @Override
  39.     public V put(K arg0, V arg1) {
  40.         if(index(arg0) == -1) {
  41.             int i = Math.abs(arg0.hashCode()%Capacity);
  42.             if(E[i]==null) {
  43.                 E[i] = new Entry<K, V>(arg0,arg1);
  44.             } else {
  45.                 Entry<K,V> inputChain = E[i];
  46.                 while(inputChain.next!=null) {
  47.                     inputChain=inputChain.next;
  48.                 }
  49.                 inputChain.next =  new Entry<K, V>(arg0,arg1);
  50.             }
  51.            
  52.             if((size*100)/E.length > 75) {
  53.                 System.out.println("rehash");
  54.                 rehash();
  55.             }
  56.             size++;
  57.             return null;
  58.         }
  59.         V vOld = find(index(arg0),arg0).value;
  60.         find(index(arg0),arg0).setValue(arg1);
  61.         return vOld;
  62.     }
  63.  
  64.     @Override
  65.     public V remove(Object arg0) {
  66.        
  67.         size--;
  68.         return null;
  69.     }
  70.  
  71.     @Override
  72.     public int size() {
  73.         return size;
  74.     }
  75.    
  76.     public String show() {
  77.         StringBuilder sb = new StringBuilder();
  78.         for(int i=0; i<E.length; i++) {
  79.             if(E[i] != null) {
  80.                 sb.append(i+"    "+E[i].toString());
  81.                 Entry<K,V> printEntry = E[i];
  82.                 while(printEntry.next != null) {
  83.                     printEntry = printEntry.next;
  84.                     sb.append(" " + printEntry.toString());
  85.                 }
  86.                 sb.append("\n");
  87.             }
  88.         }
  89.         return sb.toString();
  90.     }
  91.    
  92.     private int index(K key) {
  93.         int index =0;
  94.         for(Entry<K,V> e: E) {
  95.             if(e != null) {
  96.                 if(e.getKey().equals(key)) {
  97.                     return index;
  98.                 }
  99.                 Entry<K,V> currChain = e;
  100.                 while(currChain.next!= null) {
  101.                     currChain = currChain.next;
  102.                     if(currChain.getKey().equals(key)) {
  103.                         return index;
  104.                     }
  105.                 }
  106.             }
  107.         index++;
  108.         }
  109.         return -1;
  110.     }
  111.    
  112.     private Entry<K,V> find(int index, K key){
  113.         if(E[index]!=null) {
  114.             Entry<K,V> currEntry = E[index];
  115.             while(currEntry.next!=null) {
  116.                 if(currEntry.getKey().equals(key)) {
  117.                     return currEntry;
  118.                 }
  119.                 currEntry = currEntry.next;
  120.             }
  121.             return currEntry;
  122.         }
  123.         return null;
  124.        
  125.     }
  126.     private void rehash(){
  127.         ArrayList<Entry<K,V>> allEntries = new ArrayList<Entry<K,V>>();
  128.         for(Entry<K,V> e : E) {
  129.             if(e!=null) {
  130.                 allEntries.add(e);
  131.                 while(e.next!=null) {
  132.                     e=e.next;
  133.                     allEntries.add(e);
  134.                 }
  135.             }
  136.         }
  137.         System.out.println(allEntries.size());
  138.         Capacity = Capacity *2;
  139.         size = 0;
  140.         E = (Entry<K,V>[]) new Entry[Capacity];
  141.         for(Entry<K,V> e : allEntries) {
  142.             put(e.getKey(),e.getValue());
  143.         }
  144.     }
  145.    
  146.    
  147.    
  148.     private static class Entry<K,V> implements Map.Entry<K, V> {
  149.         private K key;
  150.         private V value;
  151.         private Entry<K,V> next;
  152.        
  153.         public Entry(K key, V value){
  154.             this.key=key;
  155.             this.value=value;
  156.             next = null;
  157.         }
  158.         @Override
  159.         public K getKey() {
  160.             return key;
  161.         }
  162.         @Override
  163.         public V getValue() {
  164.             return value;
  165.         }
  166.         @Override
  167.         public V setValue(V value) {
  168.             this.value=value;
  169.             return value;
  170.         }
  171.         public String toString() {
  172.             return key + "=" + value;
  173.         }
  174.        
  175.     }
  176.    
  177.     public static void main(String[] args) {
  178.        
  179. //      SimpleHashMap<String, Integer> SHM = new SimpleHashMap<String, Integer>();
  180. //      SHM.put("ett", 1);
  181. //      SHM.put("två", 2);
  182. //      SHM.put("tre", 3);
  183. //      SHM.put("fyra", 4);
  184. //      SHM.put("fem", 5);
  185. //      SHM.put("sex", 6);
  186. //      SHM.put("sju", 7);
  187. //      SHM.put("åtta", 8);
  188. //      SHM.put("nio", 9);
  189. //      SHM.put("tio", 10);
  190. //      SHM.put("elva", 11);
  191. //      SHM.put("tolv", 12);
  192. //      SHM.put("treton", 13);
  193. //      SHM.put("fjorton", 14);
  194. //      SHM.put("femton", 15);
  195. //      SHM.put("sexton", 17);
  196. //      SHM.put("sjuton", 2);
  197. //      SHM.put("arton", 2);
  198. //      SHM.put("niton", 2);
  199. //      SHM.put("tjugo", 2);
  200. //      SHM.put("tjugoett", 2);
  201. //      SHM.put("tjugotvå", 2);
  202. //     
  203. //      System.out.println(SHM.show());
  204. //      System.out.println(SHM.get("två"));
  205.        
  206.         SimpleHashMap<Integer,Integer> hashmap = new SimpleHashMap<Integer,Integer>();
  207.         for(int i=0; i<100; i++) {
  208.             hashmap.put(i, i);
  209. //          System.out.println(hashmap.show());
  210.         }
  211.         SimpleHashMap<Integer,Integer> m = new SimpleHashMap<Integer,Integer>();
  212.         for(int i=0; i<16; i++) {
  213.             m.put(i, i);
  214.             System.out.println(m.size +" \n"+ m.show());
  215.         }
  216.        
  217.        
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement