Java Map foreach效率

來源:互聯網
上載者:User
Java Map foreach效率

通過比較源碼和位元組碼,查看在Map 的entrySet在foreach中Java到底做了些什麼。

 

源碼:

public class ForEachTest {<br />public void inForEach(int count) {<br />Map<String, String> map = new HashMap<String, String>();<br />for (Entry<String, String> entry : map.entrySet()) {</p><p>}<br />}<br />public void outForEach(short count) {<br />Map<String, String> map = new HashMap<String, String>();<br />Set<Entry<String, String>> entrySet = map.entrySet();<br />for (Entry<String, String> entry : entrySet) {</p><p>}<br />}<br />}

位元組碼

public class com.fpi.sword.controlsdesigner.dataaccess.datamodel.design.ForEachTest extends java.lang.Object{<br />public com.fpi.sword.controlsdesigner.dataaccess.datamodel.design.ForEachTest();<br /> Code:<br /> 0: aload_0<br /> 1: invokespecial #8; //Method java/lang/Object."<init>":()V<br /> 4: return<br />public void withStrings(int);<br /> Code:<br /> 0: new #16; //class java/util/HashMap<br /> 3: dup<br /> 4: invokespecial #18; //Method java/util/HashMap."<init>":()V<br /> 7: astore_2<br /> 8: aload_2<br /> 9: invokeinterface #19, 1; //InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;<br /> 14: invokeinterface #25, 1; //InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;<br /> 19: astore 4<br /> 21: goto 35<br /> 24: aload 4<br /> 26: invokeinterface #31, 1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;<br /> 31: checkcast #37; //class java/util/Map$Entry<br /> 34: astore_3<br /> 35: aload 4<br /> 37: invokeinterface #39, 1; //InterfaceMethod java/util/Iterator.hasNext:()Z<br /> 42: ifne 24<br /> 45: return<br />public void withStringBuffer(short);<br /> Code:<br /> 0: new #16; //class java/util/HashMap<br /> 3: dup<br /> 4: invokespecial #18; //Method java/util/HashMap."<init>":()V<br /> 7: astore_2<br /> 8: aload_2<br /> 9: invokeinterface #19, 1; //InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;<br /> 14: astore_3<br /> 15: aload_3<br /> 16: invokeinterface #25, 1; //InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;<br /> 21: astore 5<br /> 23: goto 38<br /> 26: aload 5<br /> 28: invokeinterface #31, 1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;<br /> 33: checkcast #37; //class java/util/Map$Entry<br /> 36: astore 4<br /> 38: aload 5<br /> 40: invokeinterface #39, 1; //InterfaceMethod java/util/Iterator.hasNext:()Z<br /> 45: ifne 26<br /> 48: return<br />} 

 

通過比較發現:直接將HashMap.entrySet應用到for each語句中效率更高。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.