This is really weird. Here's my code:
Map<Text,Integer> frequencies = new HashMap<Text,Integer>();
Text currentBrowser = new Text();
// update map with browser frequencies
while(values.hasNext()){
currentBrowser = values.next();
if (frequencies.containsKey(currentBrowser))
frequencies.put(currentBrowser, frequencies.get(currentBrowser) + 1);
else
frequencies.put(currentBrowser, new Integer(1));
}
The idea is that values is a Iterator<Text>, and it contains a list of browsers (Chrome, IE, etc.). I simply want to create a map that will store the frequency of each browser (if Chrome appears 3 times in the list - I want its value to be 3, etc.)
The trouble is that this doesn't work. When I debug it step-by-step, here's what happens:
The first browser is Explorer, and it puts it in the map correctly:
{Explorer=1}. Next comes Safari, so it goes into the else, but this is what happens to the map: {Safari=1, Safari=1}! Next two browsers are Safari, and these it does insert appropriately: {Safari=1, Safari=3}, but then comes Firefox and this is what happens: {Firefox=1, Firefox=1, Firefox=3}. So you can see that each time a new key is inserted into the map, it also changes all other keys. The first Firefox should actually be Explorer, the second should be Firefox, and the third should be Safari.
What is happening here?
Iterable<Text>? Please post all relevant codevaluesiterator constructed? Is it possible it returns the same Text instance in each call to next()?