Simple Java implementation | Rehashing | load Factor | 80% faster
For loop on list is almost negligible as if load factor goes greater than 0.7, Set Will Rehash\n\n\nclass MyHashSet {\n\n    ArrayList<ArrayList<Integer>> set;\
* **For loop on list is almost negligible as if load factor goes greater than 0.7, Set Will Rehash**\n\n```\nclass MyHashSet {\n\n    ArrayList<ArrayList<Integer>> set;\n    int bucketSize ;\n    int size;\n    \n    public MyHashSet() {\n        bucketSize =  10;\n        size = 0;\n        set  =  new ArrayList<>();\n        for(int i=0;i<bucketSize;i++){\n            set.add(null);\n        }                \n    }\n    \n    private int getBucketIndex(int key){\n        return key % bucketSize;\n    }\n    \n    private double loadFactor(){\n        return 1.0*size/bucketSize;\n    }\n    \n    private void rehash(){ // rehash is  expensive       \n        \n        ArrayList<ArrayList<Integer>> temp =  set;\n        set  =  new ArrayList<>();\n        bucketSize*=1.5;\n        for(int i=0;i<bucketSize;i++){\n            set.add(null);\n        }\n        \n        size = 0;\n        \n        \n        for(int i=0;i<temp.size();i++ ){\n            ArrayList<Integer> li =  temp.get(i);\n            if(li!=null){\n                for(int ele :li){\n                    add(ele);\n                }\n            }\n        }\n    }\n    public void add(int key) {\n        double load  =  loadFactor();\n        if(load > 0.7){\n            rehash();\n        }\n        \n        int index =  getBucketIndex(key);\n        ArrayList<Integer> li =  set.get(index);\n        if(li==null){\n            li =  new ArrayList<>();\n            set.set( index, li  );                        \n        }\n        for(int i=0;i<li.size();i++ ){\n            if(li.get(i)==key ){\n                return;\n           }\n        }\n        \n        size++;\n        li.add(key);    \n    }\n    \n    public void remove(int key) {\n        int index =  getBucketIndex(key);\n        ArrayList<Integer> li =  set.get(index);\n        if(li==null) return;\n        \n        int idx = -1;\n        for(int i=0;i<li.size();i++ ){\n            if(li.get(i)==key ){\n                idx =  i;\n                break;\n            }\n        }\n        if(idx==-1) return;\n        \n        \n        int lastIndex =  li.size()-1;\n        \n        li.set(idx , li.get(lastIndex) ); // O(1) removal\n        li.remove(lastIndex); \n        size--;\n    }\n    \n    public boolean contains(int key) {\n        \n        int index =  getBucketIndex(key);                \n        ArrayList<Integer> li =  set.get(index);                        \n        if(li==null){            \n            return false;\n        }\n        \n        for(int i=0;i<li.size();i++ ){            \n            if(li.get(i)==key ){\n                return true;\n            }\n        }\n        \n         return false;        \n    }\n}\n\n```\n\nIt still can be Optimized to some extends for better run time this is something i will implement if asked to implement hashmap or hashset in interview, yo**ur suggestion are welcomed** \uD83D\uDC4D\uD83C\uDFFB.\n
['Java']