keySet () and entrySet ()

2008-11-19  来源:本站原创  分类:Java  人气:503 

keySet () and entrySet ()
Java code
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/ **
* Test keySet () and entrySet () of the iteration time
* KeySet (): iterative only through get () to take key
* EntrySet (): After iteration can e.getKey (), e.getValue () to take key and value. Back to the Entry interface
* Finally, we illustrate the next keySet () faster than entrySet () slow a lot. It seems they wanted to consider the use of entrySet () of the
* @ Author YL
* @ Date 2009.6.10
* /
public class HashMapTest
(
public static void main (String [] args)
(
HashMap <String,String> kmap = new HashMap <String,String> ();
HashMap <String, String> emap = new HashMap <String, String> ();

/ / Load data
for (int i = 0; i <1000; i + +)
(
kmap.put ( "" + i, "YL");
)
for (int i = 0; i <1000; i + +)
(
emap.put ( "" + i, "ZT");
)

long stimes = System.currentTimeMillis ();
long ctimes = Calendar.getInstance (). getTimeInMillis ();
long dtimes = new Date (). getTime ();

/ / Initial time here, I used three kinds of sampling method is finally found to System.currentTimeMillis (); is the most direct value method of
System.out.println (stimes + "" + ctimes + "" + dtimes);

Iterator <String> ktor = kmap.keySet (). Iterator ();
while (ktor.hasNext ())
(
System.out.println (ktor.next ());
)

long stimes1 = System.currentTimeMillis ();
long ctimes1 = Calendar.getInstance (). getTimeInMillis ();
long dtimes1 = new Date (). getTime ();

/ / End of the world and is also the start time of entrySet
System.out.println ((stimes1-stimes) + "" + (ctimes1-ctimes) + "" + (dtimes1-dtimes));
System.out.println (stimes1 + "" + ctimes1 + "" + dtimes1);

Iterator <Entry <String, String>> itor = emap.entrySet (). Iterator ();
while (itor.hasNext ())
(
Entry <String, String> e = itor.next ();
/ / System.out.println (e.getKey ());
System.out.println (e.getValue ());
)

long stimes2 = System.currentTimeMillis ();
long ctimes2 = Calendar.getInstance (). getTimeInMillis ();
long dtimes2 = new Date (). getTime ();
System.out.println (stimes2 + "" + ctimes2 + "" + dtimes2);
System.out.println ((stimes2-stimes1) + "" + (ctimes2-ctimes1) + "" + (dtimes2-dtimes1));
)
)

相关文章
  • keySet () and entrySet () 2008-11-19

    keySet () and entrySet () Java code import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; / ** * Test keySet () and entrySet () of the iteration time * KeySet (): iterative

  • Java中遍历Map的两种方法:keySet和entrySet 2012-03-20

    //方法一 Set<String> set = map.keySet(); for (String s:set) { System.out.println(s+","+map.get(s)); } //方法二 Set<Map.Entry<String, String>> entryseSet=map.entrySet(); for (Map.Entry<String, String> entry:entryseSet) { System.

  • keySet和entrySet效率比较 2012-06-01

    Map<String,String> map=new HashMap<String, String>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); map.put("4","d"); map.put("5","e");

  • Java Collections Xiangjie (a) 2010-04-11

    Java Collections Xiang Jie (1) A brief account of Java collection class is a very important basic technology content. Java collection class encapsulates a collection of commonly used data structures and algorithms, so the learning process of Java may

  • ConcurrentHashMap the implementation details 2010-09-04

    ConcurrentHashMap is Java 5, support high-concurrency, high throughput to achieve thread-safe HashMap. Before that I only have a superficial understanding of ConcurrentHashMap only know it by the number of locks, probably enough. But in an interview

  • Java Map traversal method 2011-04-26

    The first is: Map map = new HashMap (); Iterator iter = map.entrySet (). Iterator (); while (iter.hasNext ()) { Map.Entry entry = (Map.Entry) iter.next (); Object key = entry.getKey (); Object val = entry.getValue (); } High efficiency, this approach

  • Java中Map的遍历方法及性能测试 2013-12-05

    这篇文章主要介绍了Java中Map的遍历方法及性能测试,本文讲解对HashMap.TreeMap进行对比测试,给出测试代码.测试结果和测试结论,需要的朋友可以参考下 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的只是key的集合,针对每个key,都要去Map中额外查找一次value,从而降低了总体效率.那么实际情况如何呢? 为了解遍历性能的真实

  • JAVA遍历map的几种实现方法代码 2014-12-24

    这篇文章主要介绍了JAVA遍历map的几种实现方法,有需要的朋友可以参考一下 public static void main(String args[]) { Map<String, Object> map = new HashMap<String, Object>(); map.put("a", "A"); map.put("b", "B"); map.put("c", "

  • ConcurrentHashMap 2013-11-18

    A version of Hashtable supporting concurrency for both retrievals and updates: Retrievals Retrievals may overlap updates. (This is the same policy as ConcurrentReaderHashMap.) Successful retrievals using get(key) and containsKey(key) usually run with

  • java.util.List的remove()方法 2014-11-27

    public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(3); list.add(4); for (int i=0; i<list.size(); i++) { if (list.get(i) == 3) { list.remove(i);//这样写不对,需要写成

  • Guava库学习:学习Collections(六)BiMap与HashBiMap 2014-11-28

    链接地址:http://www.xx566.com/detail/145.html 首先,自我反思一下,半个月来,一直处于浑浑噩噩的状态,各种琐事缠身,很少有时间静下心来继续学习,还好现在处理的差不多了,今天起步入正轨,继续Guava库中Collections的学习,上一篇,我们学习Collections(五)Multimaps:ArrayListMultimap和HashMultimap,今天我们来看一下BiMap以及BiMap的实现HashBiMap. BiMap是Guava提供的一种key

  • ConcurrentHashMap之实现细节 2015-03-10

    ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现.在这之前我对ConcurrentHashMap只有一些肤浅的理解,仅知道它采用了多个锁 ,大概也足够了.但是在经过一次惨痛的面试经历之后,我觉得必须深入研究它的实现.面试中被问到读是否要加锁,因为读写会发生冲突,我说必须要加锁 ,我和面试官也因此发生了冲突,结果可想而知.还是闲话少说,通过仔细阅读源代码,现在总算理解ConcurrentHashMap实现机制了,其实现之精巧,令人叹服,与大家共享之

  • 有序的方式读写Properties文件 -- 对java.util.Properties的改写 2015-03-11

    目的 前几天,要做一个JSP读写Properties的模块,意在让用户能在web界面编辑系统配置.我很自然的用了java的Porperties工具类,读出写入不知多方便,但是用户说修改一个保存之后下次来就找不到自己的东西了(顺序乱了).于是需要做一个有序的Properties文件读写类.果断上stackoverflow,找了两个例子(链接不记得了),两种方式,抄下来. 方式一,LinkedProperties package com.example.common; import java.io.

  • java map的遍历操作 以及性能效率的比较 2015-03-30

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); } 效率高,以后一定要使用此种方式! 第二种: Map map = new HashMap();

  • HashMap in KeySet, EntrySet 2010-11-13

    There is a HashMap in the private inner classes - KeySet. Java code public class HashMap<K,V>{ private class KeySet extends AbstractSet<K> { .... } } KeySet implements Set interface, it seems like a new Set class. In actual fact, KeySet is dif

  • HashMap of entrySet and keySet 2010-12-27

    When you need to iterate hashmap time, please use the entrySet, rather than using keySet, entrySet keySet higher than the efficiency, in fact, use entrySet only need to traverse a hash, key and value will add the mapping to the entry, the re- taken f

  • Another way to traverse Map: Map.Entry and Map.entrySet () (turn) 2010-06-11

    Think in java today to see the GUI of this chapter, when inside TextArea This example uses a time while traversing Map Map.Entry and Map.entrySet (), remember that only met Map.KeySet () and values () These two methods So to the API, a look, Map.entr

  • With the Map.Entry and Map.entrySet () traverse the hash table 2010-08-26

    HashMap use the following format, directly to loop through the entire hashMap hashTable Back to the set each element type is a Map.Entry. private Hashtable <String, String> emails = new Hashtable <String, String> (); / / Method 1: Use entrySet

  • entrySet最高效的遍历hashmap 2011-08-04

    Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); } 还有keySet遍历方法, 效率低下.

  • Java中使用TreeMap将Map的KeySet排序 2012-12-17

    使用TreeMap简单地HashMap的key进行排序,使用ArrayList对HashMap的value排序. 分别使用Comparator接口和Comparable接口实现key的排序. package com.inspiration.examples.collection.sort; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.