Java Map: Using value object's attribute as key, bad practice? -
is bad practice utilize object's attribute key map entry? every time it, feels wrong.
for example:
class foo { string name; string bar; }
and utilize map this:
foo foo = new foo(); foo.name = "foo bar"; foo.bar = "blaaaa"; map<string, foo> foos = new hashmap<>(); foos.add(foo.name, foo);
it feels kind of wrong.
at first glance feels wrong.
but using field key reasonable, perhaps bit redundant legitimate. doesn't matter key's value comes from! because purpose of map
retrieve elements. , doing job.
if sense guilty, may remove field class if there aren't side-effects.
what may wrong - you're not using methods fields. consider next scenerio: you'd combine 2 strings new key. if have referenced field one thousand times, out of luck! have replace occurences of field. may lead bugs , inconveniences if accidentally replaced else.
but if have method, alter part in method , you're done.
java map
No comments:
Post a Comment