1

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?

11
  • 1
    What is the Text class? Is it a custom class you wrote? Can you include its code? Commented Mar 23, 2015 at 11:33
  • It's from Hadoop's org.apache.hadoop.io.Text Commented Mar 23, 2015 at 11:34
  • 1
    How do you create Iterable<Text>? Please post all relevant code Commented Mar 23, 2015 at 11:36
  • And how's the values iterator constructed? Is it possible it returns the same Text instance in each call to next()? Commented Mar 23, 2015 at 11:36
  • 1
    Obviously, this Text objet is not made to be used as part of a Map's keys, as it (or some important part of it, i.e. its contents) is mutated after it has been added into the map. You should probably use some method on this Text object, such as "to string" or "text value" to access a "stable" representation of it Commented Mar 23, 2015 at 11:38

1 Answer 1

2

From your description it looks like your problem is that the "currentBrowser" object is actually not changing. Meaning: you add the SAME object during each iteration; in addition to that, you are changing the INTERNAL representation of that object.

Long story short: I think the problem is that your iterator is returning the SAME object all the time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.