ShreyasG101

Untitled

Dec 6th, 2019
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. package hash;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.ListIterator;
  5.  
  6. import list.ListNode;
  7.  
  8.  
  9. /**
  10.  * Hash Table implementation.
  11.  *
  12.  */
  13. public class HashTable<K,V> implements Map<K,V>{
  14.  
  15.     private static final int INITIAL_CAPACITY = 100;
  16.     private int size = 0;
  17.    
  18.     /* Max allowed load factor */
  19.     private double maxLoad = 0.75;
  20.  
  21.     /* Sentinel value for removes */
  22.     private HashNode<K,V> sentinel = new HashNode<K,V>(null, null);
  23.     private LinkedList table[];
  24.     ListIterator <HashNode>nodeIterator;
  25.     public HashTable() {
  26.         this(INITIAL_CAPACITY);
  27.     }
  28.     double maximum = maxLoad;
  29.     public HashTable(int initialCapacity) {
  30.         //TODO: Write this method
  31.         //any information you want about the hash table
  32.         table = new LinkedList[initialCapacity];
  33.         size = 0;
  34.         double maximum = maxLoad;
  35.         //for(int i = 0; i<table.length; i++) {
  36.             //table[i] = new LinkedList <HashNode> ();    
  37.             //}
  38.         //to figure out index of array, compute hash
  39.         for(int i = 0; i<table.length; i++) {
  40.             if(table[i]==null) {
  41.                 //create new linked list
  42.                 table[i] = new LinkedList<>();
  43.             }
  44.         }
  45.         if(size>0) {
  46.         double loadFactor = (double)size/table.length;
  47.         }
  48.     }
  49.    
  50.     @Override
  51.     public void insert(K key, V value) {
  52.         //TODO: Write this method
  53.         //to figure out index of array, compute hash
  54.         //insert linked list at that index
  55.         //size goes up with each value added to hash table
  56.         if(size>0) {
  57.             double loadFactor = (double)size/table.length;
  58.             int nodeNumber = (Math.abs(key.hashCode()))%(table.length);
  59.             ListIterator <HashNode> nodeIterator = table[nodeNumber].listIterator();
  60.             if(loadFactor>=maximum) {
  61.                 this.resize();
  62.                 }
  63.         for(int i = 0; i<table.length; i++) {
  64.             if(table[nodeNumber]==null) {
  65.                 //create new linked list if null
  66.                 table[nodeNumber] = new LinkedList<>();
  67.                 table[nodeNumber].add(new HashNode(key,value));
  68.                 size++;
  69.             }
  70.         }
  71.             //otherwise add value
  72.         while(table[nodeNumber]!=null) {
  73.             //if key is already there, ignore
  74.             if(nodeIterator.next().getKey().equals(key)) {
  75.                
  76.             }
  77.             table[nodeNumber].add(new HashNode(key,value));
  78.             size++;
  79.         }
  80.             //else {
  81.                 //}
  82.         }
  83.        
  84.         else {
  85.             double loadFactor=0;
  86.             int nodeNumber = (Math.abs(key.hashCode()))%(table.length);
  87.             nodeIterator = table[nodeNumber].listIterator();
  88.             //otherwise add value
  89.             table[nodeNumber].add(new HashNode(key,value));
  90.             size++;
  91.             //else {
  92.                 //}
  93.         }
  94.     }
  95.  
  96.     @Override
  97.     public V retrieve(K key) {
  98.         //TODO: Write this method
  99.         int hashValue =Math.abs(key.hashCode())%(table.length);
  100.         HashNode<K,V> listNode;
  101.         ListIterator <HashNode> nodeIterator = table[hashValue].listIterator();
  102.         while(nodeIterator.hasNext()) {
  103.             if(nodeIterator.next().getKey().equals(key)) {
  104.                 return (V) nodeIterator.next().getValue();
  105.             }
  106.         }
  107.         //table[hashValue].forEach(listNode.getKey());
  108.         //ListNode<T> newNode = new ListNode <T>(data);
  109. //  for(int i = 0; i <= hashValue ;i++) {      
  110. //      if(listNode.getKey().equals(key)){
  111. //          return (V) listNode.getValue();
  112. //      }
  113. //  }
  114.     return null;
  115. }
  116.    
  117.  
  118.     @Override
  119.     public boolean contains(K key) {
  120.         //TODO: Write this method
  121.         int hashValue =(Math.abs(key.hashCode()))%(table.length);
  122.        
  123.         if(table[hashValue].listIterator().hasNext()==false){
  124.             return false;
  125.             }
  126.         else;
  127.         return true;
  128.     }
  129.  
  130.     @Override
  131.     public void remove(K key) {
  132.         //TODO: Write this method
  133.     }
  134.    
  135.     private void resize() {
  136.         //TODO: Write this method
  137.     }
  138.  
  139.     /**
  140.      * Getting and setting the maxLoad field
  141.      * @return
  142.      */
  143.     public double getMaxLoad() {return maxLoad;}
  144.     public void setMaxLoad(double maxLoad) {this.maxLoad = maxLoad;}
Add Comment
Please, Sign In to add comment