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

raul cortes
raul cortes
5,745 Points

why second parameter is this....

In this code

/if(authoredTreets == null){ authoredTreets= new ArrayList<Treet>(); treetsByAuthor.put(treet.getAuthor(),authoredTreets); }/ why are the teacher putting an empty variable into the map variable, because in that moment he is initializing authoredTreets. Its hard to me to understand that step.

Thank you a lot.

2 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Tried my best, take a look

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

for (Treet treet : treets) {

       String currentAuthor = treet.getAuthor();

      // first we try to get treets by author from the Map above

      LIst<Treet> authoredTreets = treetsByAuthor.get(currentAuthor);

      // two scenarios are possible

      // 1. authoredTreets will be null, because in the Map there are no 
      // treets mapped to "currentAuthor"
      // That is why we do the following "if"
      if (authoredTreets == null) {
             // Create empty array with treets to put in our Map.
             authoredTreets = new ArrayList<Treet>();

            // Put that empty array for key "currentAuthor"
            //  { "currentAuthor" : new ArrayList<> }
           treetsByAuthor.put(currentAuthor, authoredTreets);

          // Logical step would be to write here
          //  authoredTreets.add(treet);
          // but because for both scenarios we do the same, we can add that later
         // see below
      }

      // 2. authoredTreets already have treets with currentAuthor
      // In this case we just add treets in a map for current author
      // And this code also works if we just created empty array for
     // current author
     authoredTreets.add(treet);

    // note: we don't need to write "put" second time, because authored treets is
    // pointing to the array that is in a Map, that is why by adding treet to
    // "authoredTreets" we add it to Map as well

}

Or you know I found even better in community:

https://teamtreehouse.com/community/not-really-unterstanding-the-treetsbyauthor-method

raul cortes
raul cortes
5,745 Points

THank you a lot, I understood the Idea, I'll read the link too :)

hugs from Chile