Search code examples
javahashtable

How to convert a hash table in string in Java


I'm new in Java and I want to convert a hash table in the form of a string, with each pair separated by any special character. How can I apply loop on the hash table and extract values from?

 public String parseHashtable(Hashtable detailHashtable){
     
    String hashstring= "";
    foreach(){
    hashstring += key + "=" + hashtable[key] + "|";
    }
     return hashstring;
 }

Solution

  • String seperator = "|";
    StringBuilder sb = new StringBuilder();
    
    Set<String> keys = detailHashtable.keySet();
    for(String key: keys) {
        sb.append(key+"="+detailHashtable.get(key)+ seperator);
    }
    
    return sb.toString();