Skip to content

Instantly share code, notes, and snippets.

@culmat
Last active July 20, 2020 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save culmat/a3bcc646fa4401641ac6eb01f3719065 to your computer and use it in GitHub Desktop.
Save culmat/a3bcc646fa4401641ac6eb01f3719065 to your computer and use it in GitHub Desktop.
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