Java-Collections, cannot overwrite old value with new value -


i got question regarding java-collections. iterate through java-collection , if if-clause true, want change entry of collection. within if-clause body new value accepted, if want print whole collection later, prints out again collection old value.

here code:

public boolean checkconsumestoragecapacity(multimap<string, values> mm1,   multimap<string, values> mm2) {     boolean enoughstoragecapacity = false;       multimap<string, values> mmapp = mm1;       multimap<string, values> mmhw = mm2;       collection<values> cola = mmapp.get("storage");      collection<values> colh = mmhw.get("memory"); (values va2 : cola) {     (values vh2 : colh) {          if (va2.getname().equals("size") && vh2.getname().equals("size")) {              float stosize = float.parsefloat(va2.getvalue());             float memsize = float.parsefloat(vh2.getvalue());             float maintainablestosize = stosize * maintainabilityfactor;              if (memsize >= maintainablestosize) {                  memsize -= maintainablestosize;                 vh2.setvalue(string.valueof(memsize));                  string s = vh2.getvalue();                 system.out.println(s);                  enoughstoragecapacity = true;                 return enoughstoragecapacity;              }             break;         }      } }  system.out.println(colh); 

values object containing 3 string. getters/setters declared correctly. printing out s gives correct value, printing out colh gives again old value. isnt setting new value enough, additionally have commit in collection?

thanks lot in advance.

edit: here values class, further understanding.

public class values {  private string name;  private string type;  private string value;  public values(string name, string type, string value)  {     this.name = name;      this.type = type;      this.value = value; } public string getname()  {     return name;  } public string gettype()  {     return type;  } public string getvalue()  {     return value;  } public void setname(string name) {     this.name = name;  } public void setvalue(string value) {     this.value = value;  } public void settype(string type) {     this.type = type;  }  @override public string tostring() {     return "name=" + name + ", type=" + type + ", value=" + value;     }  } 

another way of dealing such pointer issues initialize second collection before loop. iterate through add values want stay same collection , in if statement add changed value/object. costs little more memory no real performance loss

something this:

list<t> originallist; list<t> tmp = new list for(x : originallist) { if(condition) { //do things tmp.add(changedvalue) } else {  tmp.add(x)  } } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -