title: How to create a map of character and dynamic ArrayList in Java
body: <p>I want to create a hashmap defined as <code>HashMap&lt;Character,ArrayList&lt;String&gt;&gt;</code>.
  What I am trying to do is to read a set of strings which I am reading into a list.
  From this list I want to generate this hashmap.
  So if strings are something like this</p>
  
  <p>Andy,Aman,Rocky,Ravi,Nick etc.
  Map should be like</p>
  
  <pre><code>A-&gt;[Andy,Aman]
  R-&gt;[Rocky,Ravi]
  N-&gt;Nick
  </code></pre>
  
  <p>What I have tried is something like this</p>
  
  <pre><code>ArrayList&lt;String&gt; xlist= new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      HashMap&lt;Character,ArrayList&lt;String&gt;&gt; h = new HashMap&lt;Character,ArrayList&lt;String&gt;&gt;();
      Scanner s = new Scanner(System.in);
      for(String sin : list){
          Character x =sin.charAt(0);
          //System.out.println(sin.charAt(0));
          if(h.containsKey(x)){
              h.get(x).add(sin);
              //xlist.clear();
              //xlist = h.get(x);
              //xlist.add(sin);
              //h.put(x,xlist.clone());
  
          }
          else{
              xlist.clear();
              xlist.add(sin);
              h.put(x,xlist);
  
          }
      }
  </code></pre>
  
  <p>When I print the hashmap, I get this </p>
  
  <pre><code>{A=[Ravi, Rocky], R=[Ravi, Rocky], N=[Ravi, Rocky]}
  </code></pre>
  
  <p>I understand , that all the lists in values are being reflected from same copy of list, but I don't know , how to resolve it.</p>