Custom HashMap implementation

前端 未结 7 1805
挽巷
挽巷 2021-02-06 06:45

This question was asked to me in an interview. I think the only way to get best solution is SOF. So the question was \"How would you implement a custom HashMap in java(A

7条回答
  •  有刺的猬
    2021-02-06 07:01

    public class ArrayAsHashMap {
    
        Object [][] hashArr  = new Object [10] [2];
    
        public void put(HashObject key, String value){
            int index = key.hashCode();
            Object [] arr = {key, value};
            hashArr[index] = arr;
        }
    
        public String get(HashObject key){
            int index = key.hashCode();
            Object [] arr = hashArr[index];
            return (String)arr[1];
    
        }        
    
    }
    

提交回复
热议问题