Thursday, January 15, 2015



    Convert HashMap Keys to String Array in Java

    I was working with HashMap and i wanted to convert all the keys inside the HashMap to array of String. So i tried casting the Object to String since Object is the class from where String is derived. But i got class cast exception. Started looking on what is the issue and found that you cannot directly cast the Object[] to String[]. You have to manually iterate through and create String[] array.
    Assume you have an HashMap and you get all the keyset for that HashMap
    Since the received array is of type Object, we would have to iterate through it to get the String array.
    Happy coding :)

    No comments:

    Post a Comment