Friday, June 22, 2012

java: create maps the easy way with guava

Traditional:

private HashMap indexMap = new HashMap();
indexMap.put("low", 1);
indexMap.put("medium",2);
indexMap.put("high",3);


Better (via Google Guava):

ImmutableMap indexMap = ImmutableMap.builder()
            .put("low", 0) 
            .put("medium", 1) 
            .put("high", 2) 
            .build();

source