title: Build groups from a directed graph dictionary
body: <p>Given a dictionary of a directed graph, representing nested groups and their members, return all the users for a given group.</p>
  
  <p>example-</p>
  
  <pre><code>Hashmap-
  
  key|Values
  
  Group1- [ Group3, Group5, User8, User2]
  Group2 -[ Group1, User9]
  Group3 -[ Group4, User5]
  Group4 -[ User1, User3]
  Group5 -[ User4, User7]
  </code></pre>
  
  <p>O/P should be :</p>
  
  <pre><code>group1 : [User1, User3,User5, User4, User7,User8, User2]
  group2 : [User1, User3,User5, User6, User7,User8, User9]
  group3: [User1, User3,User5]
  group4: [User1, User3]
  group5: [User4, User7]
  </code></pre>
  
  <p>I have tried in various way for example a recursive function, but I just ended scratching my head.</p>
  
  <pre><code>public static void main(String[] args) {
          HashMap&lt;String, List&lt;String&gt;&gt; hmap = new HashMap&lt;String,List&lt;String&gt;&gt;();
          List&lt;String&gt; l1 = new ArrayList&lt;String&gt;();
          List&lt;String&gt; l2 = new ArrayList&lt;String&gt;();
          List&lt;String&gt; l3 = new ArrayList&lt;String&gt;();
          List&lt;String&gt; l4 = new ArrayList&lt;String&gt;();
          List&lt;String&gt; l5 = new ArrayList&lt;String&gt;();
          l1.add("group3"); l1.add("group5"); l1.add("user8"); l1.add("user2");
          l2.add("group1"); l2.add("user9");
          l3.add("group4"); l3.add("user5");
          l4.add("user1"); l4.add("user3");
          l5.add("user4"); l5.add("user9");
          hmap.put("group1",l1);
          hmap.put("group2",l2);
          hmap.put("group3",l3);
          hmap.put("group4",l4);
          System.out.println(hmap);
          flatten(hmap);
  
      }
  
  
      public static void flatten(HashMap&lt;String, List&lt;String&gt;&gt; hmap){
          ArrayList&lt;String&gt; groups = new ArrayList&lt;String&gt;();
          for(Entry&lt;String,List&lt;String&gt;&gt; entryset : hmap.entrySet()){
              groups.add(entryset.getKey());
          }
          System.out.println(groups);
          for(Entry&lt;String,List&lt;String&gt;&gt; entryset : hmap.entrySet()){
              String s1 = entryset.getKey();
              //HashSet&lt;String&gt; set1 = new HashSet&lt;String&gt;();
              ArrayList&lt;String&gt; values = new ArrayList&lt;String&gt;();
              values.addAll(entryset.getValue());
  
          }
  </code></pre>