日志 >
技术交流
Map的了解
package map;
import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; /** * 演示各个Map的实现类 */ public class TestMap { /** * 初始化一个Map * @param map */ public static void init(Map map){ if (map != null){ String key = null; for (int i=5; i>0; i--){ key = new Integer(i).toString() + ".0"; map.put(key, key.toString()); //Map中的键是不重复的,如果插入两个键值一样的记录, //那么后插入的记录会覆盖先插入的记录 map.put(key, key.toString() + "0"); } } } /** * 输出一个Map * @param map */ public static void output(Map map){ if (map != null){ Object key = null; Object value = null; //使用迭代器遍历Map的键,根据键取值 Iterator it = map.keySet().iterator(); while (it.hasNext()){ key = it.next(); value = map.get(key); System.out.println("key: " + key + "; value: " + value ); } //或者使用迭代器遍历Map的记录Map.Entry Map.Entry entry = null; it = map.entrySet().iterator(); while (it.hasNext()){ //一个Map.Entry代表一条记录 entry = (Map.Entry)it.next(); //通过entry可以获得记录的键和值 //System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue()); } } } /** * 判断map是否包含某个键 * @param map * @param key * @return */ public static boolean containsKey(Map map, Object key){ if (map != null){ return map.containsKey(key); } return false; } /** * 判断map是否包含某个值 * @param map * @param value * @return */ public static boolean containsValue(Map map, Object value){ if (map != null){ return map.containsValue(value); } return false; } /** * 演示HashMap */ public static void testHashMap(){ Map myMap = new HashMap(); init(myMap); //HashMap的键可以为null myMap.put(null,"ddd"); //HashMap的值可以为null myMap.put("aaa", null); output(myMap); } /** * 演示Hashtable */ public static void testHashtable(){ Map myMap = new Hashtable(); init(myMap); //Hashtable的键不能为null // myMap.put(null,"ddd"); // Hashtable的值不能为null // myMap.put("aaa", null); output(myMap); } /** * 演示LinkedHashMap */ public static void testLinkedHashMap(){ Map myMap = new LinkedHashMap(); init(myMap); //LinkedHashMap的键可以为null // myMap.put(null,"ddd"); //LinkedHashMap的值可以为null myMap.put("aaa", null); output(myMap); } /** * 演示TreeMap */ public static void testTreeMap(){ Map myMap = new TreeMap(); init(myMap); //TreeMap的键不能为null //myMap.put(null,"ddd"); //TreeMap的值不能为null //myMap.put("aaa", null); output(myMap); } public static void main(String[] args) { System.out.println("采用HashMap"); TestMap.testHashMap(); System.out.println("采用Hashtable"); TestMap.testHashtable(); System.out.println("采用LinkedHashMap"); TestMap.testLinkedHashMap(); System.out.println("采用TreeMap"); TestMap.testTreeMap(); Map myMap = new HashMap(); TestMap.init(myMap); System.out.println("新初始化一个Map: myMap"); TestMap.output(myMap); //清空Map myMap.clear(); System.out.println("将myMap clear后,myMap空了么? " + myMap.isEmpty()); TestMap.output(myMap); myMap.put("aaa", "aaaa"); myMap.put("bbb", "bbbb"); //判断Map是否包含某键或者某值 System.out.println("myMap包含键aaa? "+ TestMap.containsKey(myMap, "aaa")); System.out.println("myMap包含值aaaa? "+ TestMap.containsValue(myMap, "aaaa")); //根据键删除Map中的记录 myMap.remove("aaa"); System.out.println("删除键aaa后,myMap包含键aaa? "+ TestMap.containsKey(myMap, "aaa")); //获取Map的记录数 System.out.println("myMap包含的记录数: " + myMap.size()); } /** * Map用于存储键值对,不允许键重复,值可以重复。 * (1)HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。 * HashMap最多只允许一条记录的键为null,允许多条记录的值为null。 * HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。 * 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。 * (2)Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空; * 它支持线程的同步,即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢。 * (3)LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的。 * 在遍历的时候会比HashMap慢。 * (4)TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iteraor遍历TreeMap时, * 得到的记录是排过序的。 */ } |
评论列表
/x02").replace(//[//quote/]/g,"/x01"); for(var i=0;i<2;i++) s=s.replace(//x03([^/x03/x01/x02]*?)/x02([^/x03/x01/x02]*?)/x01/g, function(a,b,c){ return '
'+b+'引用内容:'; }); return s.replace(/[/x03/x02/x01]/g,""); } var bLoaded = false; function checkMsgReply(obj) { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;checkMsgReply(obj)}, document); else checkReply(obj); if(obj.checked){ MAX_COMMENT_LEN = 500; } else { MAX_COMMENT_LEN = 4500; } _fontCount = MAX_COMMENT_LEN; //字数限制 if(!window.sendCommentEditor) return; if(sendCommentEditor.editorArea.editMode == 1) toCountFont(sendCommentEditor.id, "html"); else toCountFont(sendCommentEditor.id, "text"); } function showMsgLeftCnt() { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;showMsgLeftCnt();}, document); else showLeftSMS(); } function selectBlogPaper() { if(checkLogin() <= 10000) { top.showLoginBox("mall"); return; } if(!!top.g_JData["blogContent"]) { if(parent.g_iLoginUin == parent.g_iUin) { location.href="/qzone/newblog/blogeditor.html?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style + "&paperdialog=1"; } else { parent.location.href="http://user.qzone.qq.com/" + parent.g_iLoginUin + "/addNewBlog?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style; } } else { top.showMsgBox("抱歉,暂时无法获取该信纸信息!", 1, 2000); } } /** * 批量删除中选择全选 */ function selectAllComments(bChecked) { var oList = document.getElementsByName("commentCheckBox"); if(oList.length==0) return; for(var i=0; i 0){ dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000); delete parent.g_XDoc["delBatchReply"]; return; } dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000, 2); contentProperty.totalCommentNumber -= nDeleteCnt; //清理cache with(contentProperty){ delete parent.g_XDoc["blogRoot"].contentHSList[currentBlogid]; pageList = {}; pageIndexMap = []; currentCommentPage = lastCommentPage = (!contentProperty.nowaPage)?0:nowaPage[3]; parent.g_XDoc["blogRoot"].replyNumUpdateHSmap[currentBlogid] = totalCommentNumber; parent.isRefreshTop = true; if(currentCommentPage == 0) { setTimeout(contentInit, 1000); } else{ var tp = Math.ceil(totalCommentNumber/PAGE_COMMENT_NUM); var num = totalCommentNumber%PAGE_COMMENT_NUM; if(num==0 || currentCommentPage10000 && top.g_iLoginUin!=top.g_iUin) { $("msgboardSelfReply").style.display = ""; $("blogSelPaper").title = "我也要使用此信纸写日志"; } setTimeout(contentInit,50); //]]> </script>'+c+'
共0篇评论,第1页/共0页
批量删除 取消批量删除
共0篇评论,第1页/共0页