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

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

Why hashTagsCount.get(HashTag); and not hashTagsCount.getHashTag; w/o ()

I just can't understand why do we write hashTagsCount.get(HashTag); instead of hashTagsCount.getHashTag; Can someone please explain me that?

1 Answer

I assume the line of code you are questioning is this:

Integer count = hasTagCounts.get(hashTag);

Here hasTagCounts is a Map<String, Integer>, get is a function, and hashTag is an argument being passed to the function.

If you had a sqrt() function and you wanted to pass it a value that is stored in the variable theValue you would write sqrt(theValue), not sqrttheValue.

The parentheses are necessary to separate the function from the value being passed to it.