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


Ene Catalin
Ene Catalin
1,931 Points

Syntax not clear (maps)

In the "Java Data Structures" at maps I got lost at the highlighted line(see picture). I dont understand how the "get" method for maps works with the "count" Integer declared.

The map "hashTagCounts" is supposed to show how many times a certain hashtag was used in diferent Treets.

If someone could clarify, it would help a lot.



1 Answer

Binyamin Friedman
Binyamin Friedman
14,615 Points

Line 29: hashTagCounts is initialized as HashMap<String, Integer> where the strings are the hashtags and the integers are the number of times the hashtag was used.

Line 30-32: For each hashtag in each treet, we try retrieving the number of times the hashtag was used from our map. (Remember that the map uses the hashtags as keys).

Line 33-35: It's possible that the map doesn't have a value for a hashtag because the hashtag hasn't been used before, so we check that our count isn't null. (The map.get method will return null if the key doesn't have an entry). If the count is null then we set it to 0 because the hashtag hasn't been used before.

Line 36-37: We increase the count variable by one and insert it back into the list with the hashtag. The new entry will override the old one.