Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Object attribute while iterating over HashMap
    primarykey
    data
    text
    <p>I am using a HashMap where the key is String and Value is an object (Signal). While iterating over the Map Can I edit one of the attributes of my object before I write it to a file.</p> <p>Here is my code </p> <pre><code>public void createFile(HashMap&lt;String , Signal&gt; map, final BufferedWriter buffwriter, long totalSize) { final Iterator&lt;String&gt; iterator = map.keySet().iterator(); while(iterator.hasNext()) { String messageName = iterator.next(); Signal signal = map.get(messageName); signal.setBandwidth((signal.getSize()/totalSize)*100); csvOutput.write(signal.getSource()); csvOutput.write(signal.getName()); csvOutput.write(signal.getComponent()); csvOutput.write(Integer.toString(signal.getOccurance())); csvOutput.write(Integer.toString(signal.getSize()) ); csvOutput.write(Float.toString(signal.getBandwidth())); csvOutput.endRecord(); } } </code></pre> <p>Signal.java</p> <pre><code> public class Signal implements Comparable&lt;Signal&gt;{ String name; float bandwidth; public void setName(String name){ this.name = name; } public void setBandwidth(float bandwidth){ this.bandwidth = bandwidth; } public String getName(){ return this.name; } public float getBandwidth(){ return this.bandwidth; } @Override public int compareTo(Signal signal) { return 1; } </code></pre> <p>In the above piece of code I use messagName as key for each key in the map I get its value Try to set the bandwidth attribute and then write it to file, but it is not updating the bandwidth.</p> <p>How can I do it ? Is the only option I am left with to remove the Entry and add another with new value while iterating ?</p> <p>Thanks In Advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload