title: java.lang.ClassCastException: [Ljava.lang.Object; incompatible with [Ljava.lang.String;
body: <p>I am trying to retrieve random elements from a linkedhashset. Below is my code but it is giving me exception everytime.</p>
  
  <pre><code>private static void generateRandomUserId(Set&lt;String&gt; userIdsSet) {
  
      Random rand = new Random(System.currentTimeMillis());
      String[] setArray = (String[]) userIdsSet.toArray();
      for (int i = 0; i &lt; 10; ++i) {
          System.out.println(setArray[rand.nextInt(userIdsSet.size())]);
      }
  }
  </code></pre>
  
  <p>Below is the exception I am getting-</p>
  
  <p><code>java.lang.ClassCastException: [Ljava.lang.Object; incompatible with [Ljava.lang.String;</code></p>
  
  <p>Can anyone help me on this? And is there any better way of doing this?</p>