Skip to content

Instantly share code, notes, and snippets.

@culmat culmat/FluentHashMap.java
Last active Jun 22, 2017

Embed
What would you like to do?
package common;
/**
* <blockquote>
* <pre>
* Map<String, Integer> myMap = map("a", 1).with("b", 2);
* </pre>
* </blockquote>
*
* The first tuple determines the map type, so cast as appropriate:
* <blockquote>
* <pre>
* Map<String, Shape> myShapes = map("rect", (Shape) new Rectangle()).with("line", new Line2D.Double());
* </pre>
* </blockquote>
*/
public class FluentHashMap<K, V> extends java.util.HashMap<K, V> {
public FluentHashMap<K, V> with(K key, V value) {
put(key, value);
return this;
}
public static <K, V> FluentHashMap<K, V> map(K key, V value) {
return new FluentHashMap<K, V>().with(key, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.