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

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Stuck on treetsByAuthor

Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();

    for (Treet treet : treets) {
        List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
        if (authoredTreets == null) {
            authoredTreets = new ArrayList<Treet>();
            treetsByAuthor.put(treet.getAuthor(), authoredTreets);
        }
        authoredTreets.add(treet);   <------ problematic line of code
    }

I have no idea how the last line of code is supposed to add treet into treetsByAuthor map. How I see it is that it just adds treet to authoredTreets list which is not linked do Map in any way. Please help, I'm loosing my mind

Second question is, why this line:

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

is not outside of loop, since everytime the loop loops we want to add new item to the list, not reset it?

2 Answers

Gabbie Metheny
Gabbie Metheny
33,778 Points

Let's try and walk through what's happening in this code, one line at a time:

// Creates a new, empty Map called treetsByAuthor.
// The keys will be Strings, and the values will be Lists of Treets.
Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();

// Loops over every treet in the treets array.
for (Treet treet : treets) {

  // There's several things going on here:
    // treet.getAuthor() is using the method from the Treet class
    // to return the name of the treet's author (i.e. craigdennis);
    // treetsByAuthor.get() is attempting to 'get' a key/value pair
    // from the Map, where the key is the author (i.e. craigdennis)
    // and the value is that author's List of treets;
    // if this key/value pair exists, the List will be stored in authoredTreets
    // and the code will skip down to adding the new treet currently being looped over.
  List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());

  // If the key/value pair does not exist, because this author hasn't been looped over yet,
  // the previous line will have returned null, so this code block will run.
  if (authoredTreets == null) {

    // So this is what happens the first time an author's treet is iterated over.
    // First, a new empty List is created.
    authoredTreets = new ArrayList<Treet>();

    // Then, the Map adds a new key/value pair, where the key is the current author
    // and the value is the (currently empty) authoredTreets List we're about to add to.
    treetsByAuthor.put(treet.getAuthor(), authoredTreets);
  }

  // The current treet being looped over is added to the existing author list,
  // or the one newly created in the above 'if' block.
  authoredTreets.add(treet);
}

I hope that helps, this was certainly a gnarly bit of code to wrap your head around! Let me know if things still don't make sense!

thanks for your post, it helped me alot

Great answer thank you.

Great post, thank you.