0

I am writing a code for displaying time vs size. I am using a hashmap to make entries.The problem is that it do the calculations correctly but in the end when i try to print out the hashmap ,it does not display all the results for 7 minutes.Instead it displays the result of last entry 7 times.I have tried a lot to figure out the problem but couldn't.Plzz help me out

Here is my code

public class Analysis
{
 public static class KeepTime
 {
 String time;
 long TotalSize;
 }
 Main f1 = new Main();
 public static int j = 0;
 public static int check = 0;
 public HashMap<String, KeepTime> hMap_time = new HashMap<String, KeepTime>();
 public KeepTime TimerInstance = new KeepTime();
 void TimeBasedReporting()
 {
 String skey;
 String key;
 int k;
 int y;
 for (k = 1; k <= f1.Flows().size(); k++)
 {
 check = 0;
 j = 0;
 skey = Integer.toString(k);
 if (hMap_time.isEmpty())
 {
 //some code
 key = Integer.toString(hMap_time.size() + 1);
 hMap_time.put(key, TimerInstance);
 j = 1;
 }
 else if (check == 0 && j == 0)
 {
 for (y = 1; y <= hMap_time.size(); y++)
 {
 //some code
 }
 }
 }
 if (check == 0 && j == 0)
 {
 // some code
 key = Integer.toString(hMap_time.size() + 1);
 hMap_time.put(key, TimerInstance);
 }
 else
 {}
 }
 }
}

Following is the output

 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
 03:08:39,AM 424
asked Apr 15, 2012 at 12:14
1
  • 1
    On first glance you seem to be abusing Map. It looks like you really want to use a List. Commented Apr 15, 2012 at 12:16

3 Answers 3

2

You're creating a single instance of KeepTime and using a reference to that object as the value in various key/value pairs.

You should be creating a new instance each time you add a new entry, if you want them to be independent. (And yes, I agree with the comment that you almost certainly don't want a map here - but you'd get the same problem if you added the same reference to a list multiple times, too.)

answered Apr 15, 2012 at 12:25
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are using the one TimerInstance and place that in the map for each key

You associate the same object with all keys, so of course you wil have the same value stored in the map.

Try:

KeepTime timer = new KeepTime();

at the top of the for loop and replace all TimerInstances with timer.

You can do this without fear of changing your logic as you re-assing all members of the original TimerInstance every time you store it in the map.

Also, if you want the entries in ascending order, HashMap gives you the wrong order as it is ordered based on the hash code of the keys (String in this case). Use a TreeMap if you need proper ordering -- the default string comparison results in alphabetical ordering, or you can specify your own custom Comparator to provide the ordering you require.

answered Apr 15, 2012 at 12:25

Comments

0

The way to iterate through a map's entries is to iterate over its map.keySet() or map.entrySet(). Use one of those.

answered Apr 15, 2012 at 12:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.