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

Artur Argasinski
Artur Argasinski
1,318 Points

Why isn't treetsByAuthor.put(treet.getAuthor(), authoredTreets); at the end of the second for loop in Example.java?

if the second for loop follows the same logic as the first in Example.java we want to check whether an author already submitted one treet. If this is not the case we go into the if statement were we initialize authoredTreets as ArrayList and put it next to the corresponding author.

But if the author already has submitted a tweet we add it to our list authoredTreets.

Now comes the part I dont get. Because in my opinion now the list authoredTreets has two entries and following the same logic we should assign those two entries to the corresponding key in treetsByAuthor with:

treetsByAuthor.put(treet.getAuthor(), authoredTreets);

So that we account for the case that one author submitted multiple treets.

I would be very thankful if someone could clarify this for me :)

greetings, Artur

Bayar Bogdanov
Bayar Bogdanov
2,350 Points

Hello Artur, I think you're right, but somehow this strange code in if statement just worked.

1 Answer

I was thinking about the same thing. BUT here is the reason why I think it works. Since HashMap value is point to specific list, we can just add the new Treet into the list. So by adding a new treet to the list AND since we are not changing the pointer to that specific list, whenever we call the hashmap my the key we can get the pointer to the list that contains all the treets.