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

Gábor Tóth
Gábor Tóth
11,958 Points

Not really unterstanding the treetsByAuthor method

Hello, I know what the treetsByAuthor method should do, and why we are setting the value as a List of Treets,I also understand why we are looping, but why are we creating new Lists all over the way? Could someone explain me the whole method? It would be really nice, thank you

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I don't recall a specific 'treesByAuthor' method. Are you speaking of the section of within the public static void main(String{} args) {} that creates the 'treesByAuthor map' in Example.java? Here is an annotated version.

    // Want to create a Map of Authors to their Treets 
    // Start by creating an empty Map based on HashMap. 
    // The key is the author name (string) and value will be the list of Treets
    Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();
    // Loop through all treets....
    for (Treet treet : treets) {
      // Use the current treet's author (the key) to look up in our Map the list of treets
      // already parsed and stored in this author's treet list (the value). Assign the
      // pointer to this list to the variable 'authoredTreets'
      List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
      // The author of this tweet may not have been seen yet and hence has no Map 
      // entry yet. If not yet seen, the pointer will have a 'null' value
      if (authoredTreets == null ) {
        // Since it's 'null', we need to create a new entry in the Map
        // Create a new empty list to hold this author's treets 
        authorTreets = new ArrayList<Treet>();
        // Add new Map entry with key=this-treets-author, and value=new-empty-treets-list
        treetsByAuthor.put(treet.getAuthor(), authoredTreets);
      }
      // Now we definitely have a Map entry for this author that is either the pre-existing 
      // tweet list or our newly created empty list. In either case, 'authoredTreets' points 
      // to the list reference. Add this treet to this author's list of treets
      authoredTreets.add(treet)
    }
Bayar Bogdanov
Bayar Bogdanov
2,350 Points

Sorry but I don't get it. We only add treet in our map when we creates new ArrayList. How we put another treets to the author, which is authoredTreets != null ?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The line

        authorTreets = new ArrayList<Treet>();

Doesn't add the tweet. It only creates the arraylist map if this author not seen before.

The last line

authoredTreets.add(treet)

adds the treet to the arraylist in all cases.

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

Hi Chris,

Thanks for your explanation. But I still couldn't quite get this part : The author of this tweet may not have been seen yet and hence has no Map entry yet. If not yet seen, the pointer will have a 'null' value

What does it mean when the author may not have been seen yet?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

treetsByAuthor.get() returns a List of Treets by the author of the current trees being examined in the for loop or it returns nothing if no treets found.

If nothing is returned, meaning this if the first time the author of the current tweet has tweeted, a new array authoredTreets will need to be initialized before the current treet can be added to it.