If you debug the code, you will see that map.getClass() will not return a "HashMap" as you might expect, but an anonymous class (if you run the code in a class named Main, then the map.getClass() will be something like Main$1).
map2.getClass() will return HashMap as expected.
If you check Gson method toJson Javadocs:
This method serializes the specified object into its equivalent Json
representation. This method should be used when the specified object
is not a generic type. This method uses Object.getClass() to get the
type for the specified object, but the getClass() loses the generic
type information because of the Type Erasure feature of Java. Note
that this method works fine if the any of the object fields are of
generic type, just the object itself should not be of a generic type.
If the object is of generic type, use toJson(Object, Type) instead. If
you want to write out the object to a Writer, use toJson(Object,
Appendable) instead.
To solve your "problem" you must specify map type.
Example:
Map<String,Object> map = new HashMap<String, Object>(){{
this.put("test",1);
}};
Map<String,Object> map2 = new HashMap<>();
map2.put("test",1);
final Type type = new TypeToken<HashMap<String, Object>>() {
}.getType();
System.out.println(new Gson().toJson(map, type));
System.out.println(new Gson().toJson(map2));
Reference: https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html