java基礎--關於map的遍曆keySet和entrySet__遍曆

來源:互聯網
上載者:User

首先是一個 關於遍曆的小例子:

public static void main(String[] args) {        // TODO Auto-generated method stub        Map<String, String> maps = new HashMap<String, String>();        maps.put("111", "111a");        maps.put("222", "222b");        maps.put("333", "333c");        maps.put("444", "444d");        maps.put("555", "555e");        maps.put("666", "666f");                for(String str : maps.keySet()){            System.out.println(str + ":" + maps.get(str));        }                System.out.println("--------------");                for(Entry<String, String> str : maps.entrySet()){            System.out.println(str + "   " + str.getKey() + ":" + str.getValue());        }    }


至於這兩者的效能:

通過測試發現,第二種方式的效能通常要比第一種方式高一倍。

例子如下:

import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;/** * 測試keySet()與entrySet()的迭代時間 * keySet():迭代後只能通過get()取key * entrySet():迭代後可以e.getKey(),e.getValue()取key和value。返回的是Entry介面 * 最後說明下keySet()的速度比entrySet()慢了很多。看來以後要考慮用entrySet()了 * @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>();//裝資料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();//初始時間 這裡我用了三種取值方式 最後發現System.currentTimeMillis();是最直接的取值方法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();//結束世界並且也是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));}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.