Guava provides us with great factory methods for Java types, such as Maps.newHashMap()
.
But are there also builders for java Maps?
HashM
Here is a very simple one ...
public class FluentHashMap extends java.util.HashMap {
public FluentHashMap with(K key, V value) {
put(key, value);
return this;
}
public static FluentHashMap map(K key, V value) {
return new FluentHashMap().with(key, value);
}
}
then
import static FluentHashMap.map;
HashMap m = map("a", 1).with("b", 2);
See https://gist.github.com/culmat/a3bcc646fa4401641ac6eb01f3719065