Hashmap with maximum length? | SoloLearn: Learn to code for FREE!


Hashmap with maximum length?

I'm using a hashmap that never has to hold more than twelve elements. Is it possible to bound the size of the hashmap to prevent it from getting bigger than necessary?

1/26/2017 6:22:45 PM

Iris B

1 Answer

New Answer


You may do it with a conditional: for example, having : Hashmap<String, Integer> h = new Hashmap<String, Integer>(); if you want your hashmap to have only 12 elements, you can do this: if (h.size()<12){h.put("Hank",999);} else {System.out.println("Hash limit reached");}