Adding to a List by Value instead of reference in Java -
i have list of maps , in for-loop want add map list. heard using map.clear() has better performance creating new map problem list.add() works reference of object , using map.clear() reference not ceared.
is there possibility force list.add() use value or build other workaround?
it possible insert "by value" creating copy of map
, inserting copy. problem it's not optimization @ all.
instead of creating new empty map
, create copy of filled map
, clear original map
. means: don't avoid overhead of creating new object, introduce work of copying , clearing filled map
.
and little note on optimization in general:
it's ~10% of code that'll doing 90% of work (yes, these numbers made up, it's usual way think optimization , @ least close reality). don't over-optimize code in first run. can done , without making code less readable, run profiler , bottlenecks of code , optimize those. far more efficient , easier optimizing entire code.
Comments
Post a Comment