Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

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.

Thanks!

Imgur

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.