title: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
body: <p>I need convert HashMap to a String array, follow is my java code</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  public class demo {
  
      public static void main(String[] args) {
  
          Map&lt;String, String&gt; map1 = new HashMap&lt;String, String&gt;();
  
          map1.put("1", "1");
          map1.put("2", "2");
          map1.put("3", "3");
  
          String[] str = (String[]) map1.keySet().toArray();
  
          for(int i=0; i&lt;str.length;i++) {
              System.out.println(str[i]);
          }
      }
  }
  </code></pre>
  
  <p>when I run the code, I get the following <code>ClassCastException</code>. </p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
  at demo.main(demo.java:17)
  </code></pre>