Thursday, January 15, 2015

Being clever to avoid being clever?

I read an excellent article by Lukas Eder about the double curly braces anti pattern in Java. I've never liked the double brace trick since it puts extra load on the classloader. Is there something we can do that gets us closer to what one might be tempted to do with the double brace syntax? In java 8 we can try to use lambdas to give us a similar effect.
Map<String, Object> source = Maps.from((map) -> {
    map.put("firstName", "John");
    map.put("lastName", "Smith");
    map.put("organizations", Maps.from((orgs) -> {
        orgs.put("0", Maps.pair("id", "1234"));
        orgs.put("abc", Maps.pair("id", "5678"));
Still not as nice as if java had map literals like groovy does, but somewhat better.
Groovy example: 
def source = [
    firstName: "John", 
    lastName: "Smith",
    organizations: [
        "0":[id:1234], "abc":[id:5678]
Here is the actual code for the Maps class. What do people think? Too clever? Is there a better way?
public static class Maps {
    public static <K, V> Map<K, V> from(Consumer<Map<K, V>> target) {
        HashMap<K, V> map = new HashMap<K, V>();
        return map;
    public static <K,V> Map<K,V> pair(K key, V value){
         return Collections.singletonMap(key, value);

No comments: