Hashmap with maximum length? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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?

26th Jan 2017, 6:22 PM
Iris B
Iris B - avatar
1 Answer
0
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");}
28th Jan 2017, 9:05 AM
Luis Quezada
Luis Quezada - avatar