When you will create HashMap instance with some entries (often it used for unit-tests creation), you will write
in Java:
Map<String, Object> params = new HashMap<String, Object>();
params.put("age", 22L);
params.put("name", "whiter4bbit");
params.put("blog", "http://whiter4bbit.blogspot.com");
in Python :
params = {'age':22, 'name':'whiter4bbit', 'blog':'http://whiter4bbit.blogspot.com'}
Now compare it. What is pretty - yes Python is pretty:)
But in JDK 1.5 was introduced feature, that called 'static import'. You can import static methods from any class, for example:
import static java.lang.System.out;
And you can call out.println directly
This feature will very helpful. I use it to simplify work with java collections. About first example with HashMap, we can make map builder, that can help simplify map's creation:
public class MapUtils {
static class MapBuilder<K,V>{
private Map<K,V> map = new HashMap<K,V>();
public MapBuilder<K,V> $(K key, V value){
map.put(key, value);
return this;
}
public Map<K,V> map(){
return map;
}
}
//yeah, '$' - is allowed in identifiers:)
public static <K,V> MapBuilder $(K key, V value){
MapBuilder<K,V> mapBuilder = new MapBuilder<K,V>();
return mapBuilder.$(key, value);
}
}
Now we can use this class to simplyfy HashMap creation:
import static utils.MapUtils.$;
import static java.lang.System.out;
import java.util.Map;
public class TestMapUtils {
public static void main(String[] args) {
Map<String, Object> m = $("age", 22).
$("name", "Pasha").
$("blog", "http://whiter4bbit.blogspot.com").map();
for(String param : m.keySet()){
out.println(String.format("%s:%s", param, m.get(param)));
}
}
}
It is beautiful. Isn't it?:)
