I had a similar requirement a while ago. This has nothing to do with Guava, but you can do something similar to be able to build a Map using a free builder.
Create a base class that extends Map.
public class FluentHashMap<K, V> extends LinkedHashMap<K, V> { private static final long serialVersionUID = 4857340227048063855L; public FluentHashMap() {} public FluentHashMap<K, V> delete(Object key) { this.remove(key); return this; } }
Then create a free builder using methods that suit your needs:
public class ValueMap extends FluentHashMap<String, Object> { private static final long serialVersionUID = 1L; public ValueMap() {} public ValueMap withValue(String key, String val) { super.put(key, val); return this; } ... Add withXYZ to suit... }
Then you can implement it as follows:
ValueMap map = new ValueMap() .withValue("key 1", "value 1") .withValue("key 2", "value 2") .withValue("key 3", "value 3")
tarka Apr 13 '17 at 9:59 on 2017-04-13 09:59
source share