HashTable Construction- Part1

package HashMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable ;
import java.util.Map;
import java.util.Set;
import java.util.LinkedList ;
import java.util.Genrics.LinkedList;
public class LinkedList{
public static void main(String[] args) throws Exception {
HashTable <String,Integer>map =new HashTable<>(5) ;
map.put(“USA”,200);
map.put(“UK”,175);
map.put(“India”,300);
map.put(“China”,350);

    map.display();
}

}
class LinkedList {
private HashMap.LinkedList.Node next;

 private class Node {
     T data;
     Node next;

     Node(T data, Node next) {
         this.data = data;
         this.next = next;
     }
 }

 private int size;
 private Node head;
 private Node tail;

 public LinkedList() {
     this.size = 0;
     this.head = null;
     this.tail = null;
 }

 public int size() {
     return this.size;
 }

 public boolean isEmpty() {
     return this.size() == 0;

 }

 public void addFirst(T item) {
     Node node = new Node(item, this.head);

     this.head = node;
     if (this.isEmpty()) {
         this.tail = node;
     }
     this.size++;
 }

 public void addLast(T item) {
     Node node = new Node(item, null);
      if (this.isEmpty()) {
         this.head = node;
         this.tail = node;
     } else {
         this.tail.next = node;
         this.tail = node;
     }

     this.size++;
 }

 private Node getNode(int index) throws Exception {
     if (index < 0 || index >= this.size()) {
         throw new Exception("Index out of Range");
     }
     Node temp = this.head;
     int counter = 0;
     while (counter < index) {
         temp = temp.next;
         counter++;
     }
     return temp;
 }


 private Node addAt(int index, T item) throws Exception {
     if (index < 0 || index >= this.size()) {
         throw new Exception("Index out of Range");
     }
     if (index == 0) {
         this.addFirst(item);
     } else if (index == this.size()) {
         this.addLast(item);
     } else {
         Node temp = getNode(index - 1);
         Node node = new Node(item, temp.next);
         this.next = node;
         this.size++;
     }


     public T getFirst ()  {
         if (this.isEmpty()) {
             throw new Exception(" List is Empty");

         }
         return this.head.data;
     }

     public T gatLast ()  {
         if (this.isEmpty()) {
             throw new Exception("List is Empty");
         }
         return this.tail.data;
     }

     public T getAt ( int index )  {
         if (this.isEmpty()) {
             throw new Exception("List is Empty");
         }

         if (index < 0 || index >= this.size()) {
             throw new Exception("Index out of Range");
         }

         Node temp = this.getNode(index);
         return temp.data;
     }


     public T removeFirst ()  {
         if (this.isEmpty()) {
             throw new Exception("List is empty");
         }
         Node rv = this.head;
         if (this.size() == 1) {
             this.tail = null;
             this.head = null;
         } else {
             this.head = this.head.next;
         }
         this.size--;
         return rv.data;
     }

     public T removeLast ()  {
         if (this.isEmpty()) {
             throw new Exception("List is empty");
         }
         Node rv = this.tail;
         if (this.size() == 1) {
             this.tail = null;
             this.head = null;
         } else {
             this.head = this.head.next;
         }
         this.size--;
         return rv.data;
     }
 }
     public T removeAt ( int index) throws Exception {
         if (this.isEmpty()) {
             throw new Exception("List is Empty");
         }
         if (index < 0 || index >= this.size()) {
             throw new Exception("Index out of Range");
         }
         if (index == 0) {
             return this.removeFirst();
         } else if (index == this.size() - 1) {
             return this.removeLast();
         } else {
             Node rv = this.getNode(index);
             Node temp = this.getNode(index - 1);
             temp.next = temp.next.next;
             this.size--;
             return rv.data;
         }
     }

     public void display () throws Exception {
         if (this.isEmpty()) {
             throw new Exception("List is empty");
         }

         Node temp = this.head;
         while (temp != null) {
             System.out.print(temp.data + " =>");
             temp = temp.next;
         }
         System.out.println("END");
         System.out.println("***************************");
     }


     public int find (T data){
         int index = 0;
         for (Node temp = this.head; temp != null; temp = temp.next) {

             if (temp.data.equals(data)) {
                 return index;
             }
             index++;
         }
         return -1;
     }
 }

 class HashTable<K, V> {
     private class HTPair {
         K key;
         V values;

         HTPair(K key, V value) {
             this.key = key;
             this.value = value;
         }

         public boolean equals(Object other) {
             HTPair op = (HTPair) other;
             return this.key.equals(op.key);
         }

         public String toString() {
             return "{" + this.key + "-" + this.value + "}";
         }
     }

     public static final int DEFAULT_CAPACITY = 10;

     private LinkedList<HTPair>[] bucketArray;
     private int size;

     public HashTable() {
         this(DEFAULT_CAPACITY);
     }

     public HashTable(int capacity) {
         this.bucketArray = (LinkedList<HTPair>[]) new LinkedList[capacity];
         this.size = 0;
     }

     public void put(K key, V value) throws Exception {
         int bi = hashFunction(key);
         LinkedList<HTPair> bucket = this.bucketArray[bi];
         HTPair pta = new HTPair(key, value);

         if (bucket == null) {
             bucket = new LinkedList<>();
             bucket.addLast(pta);
             this.bucketArray[bi] = bucket;
             this.size++;
         } else {
             int findAt = bucket.find(pta);
             if (findAt == -1) {
                 bucket.addLast(pta);
                 this.size++;
             } else {
                 HTPair pair = bucket.getAt(findAt);
                 pair.value = value;
             }
         }

     }

     private int hashFunction(K key) {
         int hc = key.hashCode();
         hc = Math.abs(hc);
         int bi = hc % this.bucketArray.length;
         return bi;

     }

     public void display() throws Exception {
         for (LinkedList<HTPair> bucket : this.bucketArray) {
             if (bucket != null && !bucket.isEmpty()) {
                 bucket.display();
             } else {
                 System.out.println("NULL");
                 System.out.println("***************************");
             }
         }
         System.out.println("_______________________________");
     }
 }

compile time error aa raha hai please chech my code?

mene jo aap sir code karwaye hai me bhi wahi likhe per error aa raha hai…

okay, so problem is you messed up at certain points.
first, kindly write a mail to [email protected] to get the complete code and notes of HashMap implementation. Read about HashMaps, generic classes in java carefully, and then try to implement it yourself. and then feel free to ask me when you done writing your own code.

Now your mistakes

  1. you created multiple classes with same name
  2. you imported all standard classes like linked list, hashmap whereas we were writing our own classes for both of them(that is mentioned in tutorial as well)
  3. you messed up with generic class syntax in java

Thanks.

please resolve it and rate it if satisfied.
Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.