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 - Retired Exploring the Java Collection Framework Maps

Can anybody help in java.util.Map topic????? please... i'm really get stuck here...

Craig Dennis sir what we are doing in this code i'm not able to understand the last line why we are adding treet to authoredTreets please help me...........

Map<String, List<Treet>> treetByAuthor= new HashMap<String, List<Treet>>();
        for (Treet treet: treets) {
            List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
            if(authoredTreets == null) {
                authoredTreets = new ArrayLists<Treet>();
                treetByAuthor.put(treet.getAuthor(), authoredTreets);
            }
            authoredTreets.add(treet);
        }
Clement THOLLOT
Clement THOLLOT
1,203 Points

My guess :

so in the for loop, this line :

List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());

is there so that the list of treet authoredTreets "points" to (the list of treets written by (the author of the treet of the loop)).

with that last line, you're gonna add the treet of the loop to the treet list of the corresponding author.

I think that the tricky thing to understand is that when you go in another loop, authoredTreets is gonna point to another author's list, and you're gonna add the new treet to this different list...


I may be wrong.

1 Answer

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi Varun,

in the treetByAuthor map the name of the author gets associated with a list of treets that he created. So you have a "Map<String, List<Treet>>". The last line is just putting the treets into this list of treets.

I commented the code for you:

for (Treet treet: treets) { // Loop over all treets
            List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());  //  Get the treets by the author of the current treet
            if(authoredTreets == null) { // If author is not in the map yet
                authoredTreets = new ArrayLists<Treet>(); // Create a new treet list
                treetByAuthor.put(treet.getAuthor(), authoredTreets); // Put the new author and the treet list into the map
            }
            authoredTreets.add(treet); // And finally, add the treet to the treet list that we associated with the author
        }

Kind Regards, Florian