java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

toArray() returns an Object[], regardless of generics. You could use the overloaded variant instead:

String[] str = map1.keySet().toArray(new String[map1.size()]);

Alternatively, since a Set‘s toArray method gives no guarantee about the order, and all you’re using the array for is printing out the values, you could iterate the keySet() directly:

for (String str: map1.keySet()) {
    System.out.println(str);
}

EDIT:
Just to complete the picture, in Java 8, the foreach method can be used to make the code more elegant:

map1.keySet().forEach(System.out::println);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)