2

I have this scenario:
There is a huge text file around 500MB ,by whose contents I have to make reports.Each line have some values separated by a space.out of several values, two of which are URL and RESPONSETIME. If a url's response time is more that 8000ms I have to make a report of how many times overall that url was hit, and out of them how many times response time was greater than 8000ms, so final report will look something like this

URL total hits delayed response
url1     100     5 
url2     1000    18

I have done my part of search on google so don't suggest me that. Using a list will not be a solution because you can't modify the objects of a list while iterating through it.
Anyone please suggest ideas.

Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
kamlesh tajpuri
  • 31
  • 1
  • 1
  • 2
  • This question is not a duplicate of the question referenced above. That question is asking how to remove an item from a list. This question is asking about modifying the items held within the list. Jon Skeet's answer below states that clearly. – Night Owl Nov 16 '14 at 06:09

1 Answers1

8

Using a list will not be a solution because you can't modify the objects of a list while iterating through it.

Yes you can. You can't change the list itself (adding items, removing items or replacing items) but you can modify the objects that the list refers to. For example:

List<StringBuilder> builders = new List<StringBuilder>();
builders.add(new StringBuilder());

for (StringBuilder builder : builders) {
    builder.append("This changes the builder but not the reference in the list");
}

However, it's not entirely clear that you really need to load all the lines into a list at all. That doesn't help you aggregate by URL, which is what it sounds like you really need to do.

It feels like you should have a Map<URL, HitData>. Then read the file line by line, and any time you run into a line you have to report on, you try to fetch the corresponding entry in the map. If there isn't one, create a new one. If there is, either modify that in place, or (if you prefer immutable types as I do) create a new entry and replace the old one.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194