Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Exploring the Java Collection Framework Maps

Lucas Frixione
Lucas Frixione
1,332 Points

why doesn't the counter reset? please help

lets have an hipotetial case, 2 tweets with the following hashtags 1st "#trump #hello #trump #treet" 2nd "#trump"

in the first one #trump would be assigned to 1 since it is not in the map

then #hello would be assigned to 1 since it is not in the map

then #trump would be 2 since it is in the map and counter is equal to 1 because of #hello

THEN #treet is not on the map so the counter restarts to 1

and in the second treet the counter is set to 1 so #trump would be 2 again??

obviously this is not the case because what he wrote works, maybe i'm not understanding something that is obviously very important?

Integer count = hashTagCounts.get(hashtag);

is this somehow returning the number of times the hashtag was seen? why does it return that and not the name of the hashtag for example?? please someone help me and please explain it to me like i'm 5

2 Answers

Hey, I think this was answered in some other question. See:

Kourosh Raeen 12 months ago The value of count is determined in each iteration of the inner loop by calling the get() method on hashTagCounts. The get method is one of the methods in the HashMap class. It takes a parameter as a key, in this case hashTag, and it returns the corresponding value or null if that key is not in the map. So in this case there are two things that can happen:

1) hashTag is not in the hashTagCounts map so hashTagCounts.get(hashTag) returns null, so count is now null therefore the following if statement sets count to 0 and then count++ sets count to 1.

2) hashTag is already in the hashTagCounts map so hashTagCounts.get(hashTag) returns an integer that is 1 or larger, so since count is not null we bypass setting it to 0 and just increment it with count++.

This proccess keeps repeating for as many hash tags that treet.getHashTags() has returned. Once we are done with the inner loop then we move to the next treet in the outer loop. treet.getHashTags() returns all the hash tags for that treet and we loop over all those hash tags in the inner loop as I described above.

Hope this helps and let me know if you have any other questions about this.

Bakhtiyar Seidakhmetov
Bakhtiyar Seidakhmetov
11,936 Points

Hello!

Don't now if it is still actual. hashTagCounts.get(hashtag) returns null or value of hashtag Key. In your example, "and in the second treet the counter is set to 1 so #trump would be 2 again??" it will returns 2, because in map Key - #trump has value 2.