I got a pretty simple question but couldn't find anything so far.
I'm trying to create two class constructors.
The first constructor gets 2 Strings and one HashMap and initializes the class variables.
public Foo(String a, String b, HashMap<String, String> c) {
this.a = a;
this.b = b;
this.c = c;
}
The second constructor should only get the 2 Strings and create a "default"-HashMap.
Usually you just call this()
with the default-value inside but I could not find a way to do this with a HashMap
.
public Foo(String a, String b) {
this(a, b, new HashMap<String, String>().put("x", "y").put("f","g"));
}
Eclipse marks an error:
Type mismatch: cannot convert from
String
toHashMap<String,String>
And otherwise the this()
-call cannot be the first statement in the function.
public Foo(String a, String b) {
HashMap<String, String> c = new HashMap<String, String>();
c.put("x", "y");
c.put("f", "g");
this(a, b, c);
}
Any ideas how to solve this?
Worst case I had to duplicate the code, but I was wondering if there is no better way.