Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package examples;
 import org.mapdb.*;
 import java.util.Map;

Keep tracks of number of items in map.

Collections.size() typically requires traversing entire collection in MapDB, but there is optional parameter which controls if Map keeps track of its count.

public class Map_Size_Counter {
    public static void main(String[] args) {
        //first option, create Map with counter (NOTE: counter is not on by default)
        DB db1 = DBMaker.newTempFileDB().make();
        Map m = db1.createHashMap("map1a")
<<here is counter argumen
        m = db1.createTreeMap("map1b")
<<here is counter argumen
        //second option, create external Atomic.Long and bind it to map */
        DB db2 = DBMaker.newTempFileDB().make();
        BTreeMap primary = db2.getTreeMap("map2");
        Atomic.Long sizeCounter = db1.getAtomicLong("mapSize");
        primary.put("111""some value");
New to GrepCode? Check out our FAQ X