Db4o-based Java Map

Source: Internet
Author: User
Java map is the most commonly used container interface. It stores data through flexible key/value structures. Because map is easy to use, many engineers prefer to use map to store data in large quantities. However, map data is stored in the memory, and a large amount of data is stored in the memory, which is not only a worry about slow memory traversal in the Java system, but also a system memory overflow. To solve this problem, we re-use db4o to implement the map interface, and use the efficient physical storage feature of db4o objects to reduce the memory pressure of Java. The logical diagram is as follows:


Operation example
Package COM. shine. framework. db4o. example; import Java. util. hashmap; import Java. util. map; import COM. shine. framework. db4o. util. db4omap; public class db4omapexample {/*** @ Param ARGs */public static void main (string [] ARGs) {/** ============= Simple Object Storage Service =============== */db4omap <string, object> map = new db4omap <string, Object> (true); // Add data system. out. println (map. put ("key1", "value1"); system. out. println (map. put ("key1", "value2"); system. out. println (map. put ("key1", "value3"); system. out. println (map. put ("key1", "value4"); system. out. println (map. put ("key2", "value2"); system. out. println (map. put ("key3", "value3"); system. out. println (map. put ("key4", "value4"); // check the data system. out. println (map. containskey ("key1"); system. out. println (map. containsvalue ("value1"); system. out. println (map. isempty (); // obtain a single data entry system. out. println (map. get ("key2"); // delete a single data entry system. out. println (map. remove ("key1"); system. out. println (map. remove ("key4", "value4"); // deletes all data maps. removeall ("key2"); // query all data for (Object OBJ: map. getall () {system. out. println (OBJ);} // traverses all keys for (string key: map. keyset () {system. out. println (key);} // traverses all values for (object Value: map. values () {system. out. println (value);} // traverses all key-value pairs for (map. entry <string, Object> entry: map. entryset () {system. out. println (entry. getkey () + ":" + entry. getvalue ();} // clear all data map. clear (); // query the total number of system. out. println (map. size (); // release the resource map. destroy ();/** ============= advanced Object Storage Service =============== */db4omap <string, map <string, string> MAP2 = new db4omap <string, Map <string, string> (); // Add Data Map <string, string> data1 = new hashmap <string, string> (); data1.put ("data1.key1", "data1.value1"); data1.put ("data1.key2", "data1.value2"); Map <string, string> data2 = new hashmap <string, string> (); data2.put ("data2.key1", "data2.value1"); data2.put ("data2.key2", "data2.value2"); Map <string, string> data3 = new hashmap <string, string> (); data3.put ("data3.key1", "data3.value1"); Map <string, string> data4 = new hashmap <string, string> (); data4.put ("data4.key1", "data4.value1"); data4.put ("data4.key2", "data4.value1"); data4.put ("data4.key3", "data4.value1 "); map2.put ("map1", data1); map2.put ("MAP2", data2); map2.put ("map3", data3); map2.put ("map3", data4 ); // check the data system. out. println (map2.containskey ("map1"); system. out. println (map2.containsvalue (data1); // obtain a single data entry system. out. println (map2.get ("map1"); // remove a single data entry system. out. println (map2.remove ("MAP2"); // remove the specified key-value pair system. out. println (map2.remove ("map1", data1); // traverses all keys for (string key: map2.keyset () {system. out. println (key);} // traverses all key-value pairs for (map. entry <string, Map <string, string> entry: map2.entryset () {system. out. println (entry. getkey () + ":" + entry. getvalue ();} // traverse all data for (Map <string, string> datas: map2.getall () {for (map. entry <string, string> entry: datas. entryset () {system. out. println (entry. getkey () + ":" + entry. getvalue () ;}/// clear all data map2.clear (); // query the total number of system. out. println (map2.size (); map2.destroy ();}}

Thanks to my colleague qun1988@gmail.com, if you have any problems, you can contact him.
SVN address: http://code.google.com/p/ken-javaframeword/source/browse/trunk/JavaFramework2.5/src/com/shine/framework/DB4o/example/DB4oMapExample.java
Interested in javaframework2.5 this SVN downloading http://code.google.com/p/ken-javaframeword/source/browse/trunk/JavaFramework2.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.