Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package examples;
 import org.mapdb.Bind;
 import org.mapdb.Fun;
Shows how to split map into categories and count elements in each category Here we show histogram of an Math.random(). We represent category as string for clarity, but any number or other type could be used
public class Histogram {
    public static void main(String[] args) {
        HTreeMap<LongDoublemap = DBMaker.newTempHashMap();
        // histogram, category is a key, count is a value
        ConcurrentMap<StringLonghistogram = new ConcurrentHashMap<StringLong>(); //any map will do
        // bind histogram to primary map
        // we need function which returns category for each map entry
        Bind.histogram(maphistogramnew Fun.Function2<StringLongDouble>(){
            public String run(Long keyDouble value) {
                if(value<0.25) return "first quarter";
                else if(value<0.5) return "second quarter";
                else if(value<0.75) return "third quarter";
                else return "fourth quarter";
        //insert some random stuff
        for(long key=0;key<1e4;key++){
            map.put(key, Math.random());
New to GrepCode? Check out our FAQ X