Search code examples
javahashmapload-factor

how to change the hashmap load factor


We know that hashmap has default load factor 0.75, and if I want to change it how to do that.

Is there any method so that we can set and use the load factory. I have 100k records and I don't want to rehashing again and again, I want to change the load factor so that it can work efficiently without rehashing.


Solution

  • you can not change that after the map is created, the most you can y use the constructor defined for that

    as the doc states:

    public HashMap(int initialCapacity, float loadFactor)
    

    Constructs an empty HashMap with the specified initial capacity and load factor.

     Map<String, String> x = new HashMap<>(10, 0.85f);