How to fill in this HashMap neatly without code duplication? I'm supplying a HashMap with data in the following way:

HashMap&lt;String,MyClass&gt; myHashMap = new HashMap&lt;String,MyClass&gt;();
myHashMap.put( "foo", new MyClass(2,4) );
myHashMap.put( "bar", new MyClass(0,10) );
myHashMap.put( "a",   new MyClass(0,0) );
myHashMap.put( "b",   new MyClass(0,1) );
myHashMap.put( "c",   new MyClass(0,0) );
myHashMap.put( "d",   new MyClass(0,42) );
// ...


There's around 20 lines, but a lot is duplicated text. The only interesting information in every line is foo,2,4, bar,0,10, a,0,0 and so on. How can I make this code neater?

If only the strings differed, I'd do it this way:

List&lt;String&gt; names = Arrays.aslist("foo","bar","a","b","c","d");
for ( String name : names )
    myHashMap.put( name, new MyClass(0,0) );
Then I would use a static method to put the entries in the map:

HashMap&lt;String,MyClass&gt; myHashMap = new HashMap&lt;String,MyClass&gt;();
addEntry(myHashMap, "foo",2,4);
addEntry(myHashMap, "bar", 0,10);
addEntry(myHashMap, "a", 0,0);
addEntry(myHashMap, "b", 0,1);
addEntry(myHashMap, "c", 0,0);
addEntry(myHashMap, "d", 0,42);
// ...

private static //
void addEntry(HashMap&lt;String,MyClass&gt; map, String name, int arg1, int arg2){
  map.put( name, new MyClass(arg1,arg2) );
}


I recommend reading the data from a file and call addEntry(myHashMap, ...); in a loop until end of file is reached.