javaarraylisthashtable

how to connect a Arraylist to one Index from an another Arraylist


im a bigginer for coding and java. Im now learning about Hashtable.

What im struggling here ist to connect a ArrayList to one of the index from an another ArrayList. a simple image of what I want to implement(image) and i have a difficulty to make a code

class HashTable { 
    private int size;
    private ArrayList<Pair> hashTable;
    Pair Pair;  

    // Constructor
    public j(int m) {
        Pair = null;
        size = m;
        hashTable = new ArrayList<>(m);   
        for (int i = 0; i < m; i++) {           
           // hashTable.add(null);
            hashTable.add(Pair);
        }
    }
private static class Pair {  
        Integer key;
        Integer value;

        public Pair(Integer key, Integer value) {
            this.key = key;
            this.value = value;
        }
  }
private int addressOfList(Integer key) {
        int index = Math.floorMod(key, size);
        return index;
    }

public void insert(Integer key, Integer value) {
        int index = addressOfList(key);             
        Pair existingPair = hashTable.get(index);
        
        Pair newPair = new Pair(key, value);
        ArrayList<Pair> listAtIndex = new ArrayList<>();

        // if there is not jet a Pair in hashTabel.get(index)
        if(existingPair == null) {
            //connect listAtIndex to an index                    
            hashTable.set(index, newPair);  // here is where im having problems      
            listAtIndex.add(0, newPair); 
            
        }else if(newPair.key.equals(existingPair.key)) {
            listAtIndex.add(0, newPair);
        }        
   }
}

so if I explain my code, I wanted to connect listAtIndex with the one of the index from hashTable.

so I fist began with writing in the insert Method hashTable.set(index, listAtIndex); But of course there's a Syntax error, because the second parameter in set-Method need to be an Object(if im clear..)

can anyone help me?


Solution

  • You've a lot of issues in the code and also in the logic you are trying to create. I've skimmed through the code and tried to understand

    You are basically trying to create a Map<T,V> using a List<Pair>.

    Also, you've tried to implement you hash function in your case its addressOfList which is also wrong. A better function would be Objects.hash(key).

    Furthermore, in the constructor you're initializing the list with null elements. There is no need to do so.

           for (int i = 0; i < m; i++) {           
               // hashTable.add(null);
                hashTable.add(Pair);
            }
    

    Also, you are creating a local list which will lose its reference after the function call is over initializing it with newPair listAtIndex.add(0, newPair); which is meaningless.

    Suggestion

    I've created one record which is perfect for your example:

    record Pair<T, V>(T key, V value) {
    
        @Override
        public int hashCode() {
            return Objects.hash(key);
        }
    }
    

    You can replace your default inner static class with this record.

    Also, you don't need private int size; you can check the size by hashTable.size() anywhere.