java - Any way to make several object initializations more compact? -
i have next several hashmap initializations (previously defined elsewhere):
hm1 = new hashmap<string, integer>(); hm2 = new hashmap<string, integer>(); . . . hmn = new hashmap<string, integer>();
one thought store references array , create objects within loop, allocate unnecessary space. ideas?
one thought store references array , create objects within loop, allocate unnecessary space. ideas?
the array utilize 1 word each hashmap reference , ~3 words object header. trivial compared space occupied hashmap objects.
i think doing here (in dismissing array-based solution) "premature optimization".
fwiw, can't think of better approach1 using array of hashmap objects. arraylist
alternative improve if number of hashmap
objects dynamic, replacing grouping of instance variables, offers no clear advantages use-case2.
1 - utilize reflection, code unnecessarily complicated, , liable fragile. besides, may end using more space represent executable code save in space represent array. not recommend approach.
2 - , uses more space, though shouldn't on agenda.
java initialization
No comments:
Post a Comment