Platinum Partner

Java HashMap Litteral Creator

// Here is a nice helper to create on the fly litteral hashmap.
// this makes use of Java 1.5 generic methods / class parametrization. 
//
// Example of usage :
//    
// import static Utils.*
// 
// ... 
// 
// HashMap = map("toto", 2, "titi", 3)
//
//

class Utils {

    /** Create a quick litteral map */
    public static  HashMap map(K key1, V val1, Object ... others) {
        
        // Create result hashmap
        HashMap map = new HashMap();
        
        // Add first key/val pair
        map.put(key1, val1);
        
        // Check number of args
        if (others.length % 2 != 0) throw new Error("There should be a even number of arguments");
        
        // Loop on pairs
        for (int i=0; i < others.length % 2; i++) {
            
            // Get key / val
            K key = (K) others[2*i];
            V val = (V) others[2*i + 1];
            
            // Add it to the map
            map.put(key, val);
          
            
        } // End of loop on pairs
        
        return map;    
    }
}
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}