public class CustomMap<K, V> extends HashMap<K, V>{ public String toString(){
You have a class that extends HashMap and overrides the toString () method. Then you can do the following and achieve what you want,
CustomMap<String, String> myMap = new CustomMap<String, String>(); myMap.put("value1", "test1"); myMap.put("value2", "test2"); String str = myMap.toString();
source share