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

Why does "hashTagCounts.get(hashTag);" work?

Hi guys,

I'm not stuck (for a change) but I don't really get something. It's the following line:

Integer count = hashTagCounts.get(hashTag);

I get the whole idea of what Craig is doing here. It took me a few tries and a few re-reads, but I get the point of what is happening in this video. I understand what the line I'm referring to is doing. But I don't get why it's working.

To explain my question, I need to give you a insight in my mind I'm afraid. I'm not really good with the terms and stuff. But here is how I .. euh .. 'do Java' haha.

  1. Okay, we want to create a new Map called 'hashTagCount' and we're going to store the different hashtags (key) in there, and how often they where mentioned (value).

  2. We're going to loop through all the treets, and loop again through all the hashtags. All these hashtags will be stored in 'hashTag'.

  3. And in the last line it says: hashTagCounts.put(hashTag, ---); So all the different hashtags will be stored in the map als key.

So far, I understand . Butttt,

Now, let's create an Integer called count, and we'll check all the treets for one of the hashtags and if it's found, let's add 1 to the count.

I get the idea of what it is doing. But why are we using the name of the Map here? The Map 'hashTagCounts' we created a few lines above is empty right? All we did was say what it should be named, and what kind of data (String, Integer) we are storing there.

To me, it would make a lot more sense if we would do something like check if 'hashTag' == 'hashTag' (Okay, that last one is just some pseudocode okay. I just started learning Java.)

Who could help me out explaining this?

Thanks in advance,

Bart van Ackooij

4 Answers

Amanda Lam
Amanda Lam
2,178 Points

The "hashTagCounts.get(hashTag);" line is checking to see whether the hash tag is in the map. If it's not in the map, then we need to start a new count for this hashtag and start the count at 0. If it is in the map, we just need to increase the count by one.

So, going through the loop:

Since the map is empty in the beginning and the hash tag isn't in the map (so count = null), then the count variable is initialized at 0. After this, we then add one to the count with "count++" and put this count into our map. That is the end of the first loop. After the first loop, the map hashTagCounts isn't empty anymore. Therefore, when it goes through the loop a second time and again does the "Integer count = hashTagCounts.get(hashTag);", the "if (count == null)" isn't true anymore and it just skips straight to count++. This increases the count by one, and stores the new count in the map with the same key.

Hope I explained it well enough for you to understand!

Hey Amanada,

Thanks for your reply, I think I kind of get it now. It´s quite confusing; the dubble loop, but I think it´s sinking in a bit. I will do the course again in a few days just to see if I really get it. But it does make a bit more sense now. The sentence that did it was 'After the first loop, the map isn't empty anymore'.

Thanks for taking the time to reply!

Bart

Great explanation!

Myungsuk Han
Myungsuk Han
1,399 Points

I am not a native English speaker. If there is any mistake in English as well as explanation, please correct me!

Map<String, Integer> hashTagCounts = new HashMap<String, Integer>();
 //create a Map interface type object. the name is hashTagCounts

      for(Treet treet : treets){
    //read each value from treets, assign the value to treet 
    //and repeat it(reading and assigning) as many times as the number of treets

        for (String hashTag : treet.getHashTags()){
        // from the treet value that you got from the above 'for loop', check if there is any hashtag. 
                // if any, assign it the hashTag String, and
        // repeat it( checking(or more exactly getting) and assigning) 
                // as many times as the number of hashtags in treets
        // if there is no hashTag in the treet, go to the first 'for loop'

            Integer count = hashTagCounts.get(hashTag);
            //You should know that an object which implements hashMap of Map collection has Map.Entry objects. 
            //And, each object has a key object and a value object in it.
            //And, hashTagCounts above code has a certain hashTag as its key 
                        //and the frequency of the hashTag as its value.
            //get() method here is used to get the key(=frequency of appearing in the tweet) 
                        //of the given value(hashTag)
            //and store the key to count variable of which the type is int.

            if (count == null){
            // count==null means that the hashTag above appeared the first time.

            count=0;
            // then, initialize and assign 0 to the count variable.
            }

            count++;
            hashTagCounts.put(hashTag, count);
            //assign hashTag and count to the hashTagCounts object.
            //even if the same hashTag exists it is replaced with the later (same) hashTag 
                        //and the count is updated with count++.

        }
        // go up to the second 'for loop' to process the next hashTag, if any.
        // if there is no hashTag in the tweet we are reviewing, 


    }//  go over to the next tweet( which means to finish the second 'for loop'
        // and start to review the next hashTag from the first 'for loop')

In short, the first 'for loop' is to see whether any hashTag exists in a treet, and if it exists, the second 'for loop' tries to update the frequency of hashTag.

Hi guys,

Just wondering if anybody can help me out. I still don't understand it, and now I'm running into the same thing again.

Thanks in advance,

Bart van Ackooij

Artur Argasinski
Artur Argasinski
1,318 Points

Hey Bart, I see that my answer comes a bit late but may be it still can be of some use to you.

At the beginning the Map hashTagCounts is empty.

The first loop of the nested for loop initializes count and puts the first hashtag of treet with count = 1 into the map hashTagCounts

The second loop of the nested for loop checks if the second hashtag of treet is in the map hashTagCounts: now there are two possibilities 1) the second hashtag is not in the Map hashTagCounts, which means count would be null and initialized to 0 and your new hashtag would again get the count 1 after count beeing incremented 2) if the second hashtag would be in the Map hashTagCounts it would return the integer value associated to the key of the hashtag and increment it by one. Then it would save the new count of your hashtag in your map hashTagCounts.

this would go on untill you went through all the hashtags in your first treet. the first foor loop would skip to the second treet and youl would go through this process again. I hope i could help you somehow.