java性能:将hashMap值存储在变量中是多余的吗?

java performance : Is storing a hashMap value in a variable redundant?(java性能:将hashMap值存储在变量中是多余的吗?)

本文介绍了java性能:将hashMap值存储在变量中是多余的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是减少内存使用.如果我需要多次使用该值,我应该将 hashMap 值存储在变量中吗?

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() {
          String prodUPC = pHashMap.get("productUpc");
          if(prodUpc == ''){
            //do stuff;
          }
          else if(prodUpc == '0123456'){
            //do other stuff;
          }
    }

或者我应该总是使用 hashMap 的 get() 方法来避免冗余和不必要的内存使用吗?

Or should I just always use the hashMap's get() method to avoid redundancy and unecessary memory usage?

 public void upcCheck() {

              if(pHashMap.get("productUpc") == ''){
                //do stuff;
              }
              else if(pHashMap.get("productUpc") == '0123456'){
                //do other stuff;
              }
        }

hashMap 包含很多值 ae:产品类型、产品价格等...许多方法都设置为使用这些值.所以我想知道最好的方法.谢谢!

The hashMap contains alot of values ae: product type, product price etc... Many methods are set to work with those values. So I was wondering about the best approach. Thank you!

推荐答案

不是多余的,也不会占用太多额外的内存.额外的变量不会占用太多内存;正如 this 帖子所述,没有标准数量(取决于虚拟机),但数量足够小,不会担心.

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.

在这种情况下,如果您使用的是 List,那么根据数组​​的大小查看访问该列表的时间会如何增加会很重要.这种时间测量称为时间复杂度.不过,由于您使用的是 Map,所以不必担心.

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.

这篇关于java性能:将hashMap值存储在变量中是多余的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:java性能:将hashMap值存储在变量中是多余的吗?

基础教程推荐