3

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.

Here is my code

public void createFile(HashMap<String , Signal> map, final BufferedWriter buffwriter, long totalSize) {
    final Iterator<String> 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();               
    }
 }

Signal.java

  public class Signal implements Comparable<Signal>{    
      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;
   }

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.

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 ?

Thanks In Advance

Wearybands
  • 2,438
  • 8
  • 34
  • 53

2 Answers2

2

Let me guess, your bandwidth stays 0? That's because of the way you calculate it. I assume that getSize() returns an int/long, and totalSize is an int/long. This results in the result of your calculation

(signal.getSize()/totalSize)*100

being an int as well. Try the following:

(signal.getSize() / (float) totalSize) * 100

Now one of the operands is a float, what makes the result of the calculation a float as well. Hope this resolves your problem.

See also here.

Community
  • 1
  • 1
Sven Amann
  • 565
  • 2
  • 12
0

OK, firstly you CAN modify any object state by reference, so, you got reference to Signal and you can set what do you want (if that object mutable), that one of the reason why I asked to provide code of the class.

I tested your example like next

 Signal signal = new Signal();
    signal.setName("1");
    signal.setBandwidth(23);
    HashMap<String , Signal> map = new HashMap<String , Signal>();
    map.put("1", signal);
    final Iterator<String> iterator = map.keySet().iterator();
    while(iterator.hasNext()) {
      String messageName = iterator.next();
      signal  = map.get(messageName);
      signal.setBandwidth(1000);
      System.out.println(signal.getBandwidth());
    }
  }

and in result I will see 1000.0

BTW try to use EntrySet insted of keySet + get

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
  • The answer provided by salsolatragus worked for me. Is it possible I sort my entries on the basis of bandwidth, the higher the bandwidth first it should come ? – Wearybands Sep 24 '13 at 10:35
  • It is possible using external sort or use TreeMap and bandwidth as key – Sergii Zagriichuk Sep 24 '13 at 10:42
  • How can I use a TreeMap and bandwidth as key to sort all my entries on basis of bandwidth ? Can you please provide some code ? – Wearybands Sep 24 '13 at 10:46
  • 1) Create TreeMap; 2) Loop through all data(values) from HasMap; 3) Put values to TreeMap where key is bandwidth and value instance of Signal; Like that – Sergii Zagriichuk Sep 24 '13 at 10:54