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;
}
}
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;
}
}
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!
getcall to your hashmap. Although seeing how accessing a hashmap isnt as expensive as accessing a big list, its still easier to read. Prefer readability. If you're worried about reference variables taking up stack space, check out this postProduct. Java is an OO language. Also, your code doesn't compile, and this is not how you compare Strings in Java.