0

I am creating a custom map.

This is a class of node

class HashNode<K, V> {
    K key;
    V value;
    HashNode<K, V> next;

    public HashNode(K key, V value) {
        super();
        this.key = key;
        this.value = value;
        this.next = null;
    }
}

Before adding this HashNode object in arraylist next is null. Retrieving the same object from arraylist after adding I am getting infinite recursion.

public class CustomMap<K, V> {

    private ArrayList<HashNode<K, V>> bucketArray;
    private int capacity;
    private int size;

    public CustomMap() {
        this(10);
    }

    public CustomMap(int capacity) {
        this.bucketArray = new ArrayList<HashNode<K, V>>(capacity);
        this.capacity = capacity;
        this.size = 0;

        // initializing with null all the bucket
        for (int i = 0; i < capacity; i++) {
            bucketArray.add(null);
        }

    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int getIndex(K key) {

        // Hash code
        int hashCode = key.hashCode();

        // Compressor.
        int index = hashCode % capacity;
        return index;
    }

    public V get(K key) {

        int index = getIndex(key);
        HashNode<K, V> head = bucketArray.get(index);

        while (head != null) {

            // We are comparing key which will be unique in whole map

            if (head.key.equals(key)) {
                return head.value;
            }
            head = head.next;
        }

        return null;
    }

    public void put(K key, V value) {

        int index = getIndex(key);
        HashNode<K, V> head = bucketArray.get(index);
        HashNode<K, V> node = new HashNode<K, V>(key, value);
        // update value if key already exists
        while (head != null) {
            if (head.key.equals(key)) {
                head.value = value;
                return;
            }
            head = head.next;
        }
        if (head == null) {
            bucketArray.set(index, node);
        }

        // if some other node is available at the same index

        head = bucketArray.get(index);
        while (head.next != null) {
            head = head.next;
        }

        head.next = node;
        size++;

        //making capacity of hashmap double if load factor reach to 5
        if ((1.0 * size) / capacity > 5) {
            size = 0;
            ArrayList<HashNode<K, V>> temp = bucketArray;
            capacity *= 2;
            bucketArray = new ArrayList<HashNode<K, V>>(capacity);

            for (int i = 0; i < capacity; i++) {
                bucketArray.add(null);
            }

            for (HashNode<K, V> hashNode : temp) {
                while (hashNode != null) {
                    put(hashNode.key, hashNode.value);
                    hashNode = hashNode.next;
                }
            }

        }

    }

    public HashNode<K,V> delete(K key) {
        int index = getIndex(key);
        HashNode<K,V> head=bucketArray.get(index);

        if(head==null) {
            return null;
        }else {

            if(head.key.equals(key)) {
                bucketArray.set(index, null);
                size--;
                return head;
            }else {
                while(head!=null) {
                    if(head.next.key.equals(key)) {
                        HashNode<K,V> temp=head.next;
                        head.next=head.next.next;
                        size--;
                        return temp;
                    }
                }
            }
        }

        return null;
    }

}

In the delete function, i was debugging the code and i found HashNode head=bucketArray.get(index); head.next is not null and it storing itself. The below image will show the problme.

enter image description here

Why next is getting infinite recursion of itself.

It's a kind of custom map created .

CustomMap<String, String> map = new CustomMap<String, String>(10);

        map.put("0126ca121001", "Ajay");
        map.put("0126ca121002", "Aakash");
        map.put("0126ca121003", "Prashant");
        System.out.println(map.delete("0126ca121001")); 

But at the moment I am calling map.delete("0126ca121001")); it retrieving the object using index from array list . And for that object
key="0126c121001" , value="Ajay" and next should be null. But unfortunately next is not null. next is going to be infinite recursion of itself.

Ajay Kumar Jaiswal
  • 352
  • 1
  • 6
  • 24

0 Answers0