Guava provides us with great factory methods for Java types, such as Maps.newHashMap().
But are there also builders for java Maps?
HashM
This is something I always wanted, especially while setting up test fixtures. Finally, I decided to write a simple fluent builder of my own that could build any Map implementation - https://gist.github.com/samshu/b471f5a2925fa9d9b718795d8bbdfe42#file-mapbuilder-java
/**
* @param mapClass Any {@link Map} implementation type. e.g., HashMap.class
*/
public static MapBuilder builder(@SuppressWarnings("rawtypes") Class extends Map> mapClass)
throws InstantiationException,
IllegalAccessException {
return new MapBuilder(mapClass);
}
public MapBuilder put(K key, V value) {
map.put(key, value);
return this;
}
public Map build() {
return map;
}