java performance : Is storing a hashMap value in a variable redundant?

后端 未结 4 861
误落风尘
误落风尘 2020-12-20 07:35

my goal is to reduce memory usage. should I store a hashMap value in a variable if I need to use that value multiple times?

public void upcCheck() {
                 


        
4条回答
  •  粉色の甜心
    2020-12-20 08:31

    It's not redundant, and doesn't take a lot of extra memory. Extra variables don't take much memory; as this post mentions, there is no standard amount (it depends on the vm), but the amount is small enough to not worry about.

    In a situation like this, if you were using a List, it would matter, seeing how accessing that list can increase in time depending on the size of the array. This measurement of time is called the time complexity. Although, since you're using a Map, it's not something to worry about.

    I would prefer using the extra variable, since it's personally easier to read. It's always best to prefer readability. Removing the variable will not cause any noticable performance boosts, seeing how it doesn't take much memory at all, and shouldn't be something to worry about.

提交回复
热议问题