<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ![](https://cdn.zimug.com/wx-zimug.png) **Java8 forEach**是一個工具方法用于遍歷集合,比如: (list, set or map) 和stream流(java8 提供的另外一個特性),然后對集合中的每一個元素執行特定的操作。 ## 1. Java 8 forEach 方法 #### 1.1. Iterable.forEach()方法 下面的代碼片段顯示了Iterable接口forEach方法的默認實現。我們可以通過這個方法去遍歷除了Map之外的所有集合類。 ![](https://img.kancloud.cn/cf/ce/cfcebc1645f035a9597d6f0d120f810a_464x181.png) 上面的方法對Iterable的每個元素執行操作,直到所有元素都已處理或該操作引發異常。“ action”用來表示一個接受單個輸入參數且不返回結果的操作。它是“Consumer”接口的一個實例。 ![](https://img.kancloud.cn/f6/ac/f6acbe2b8fb14f718a7e2f3cf940e980_568x218.png) 我們可以通過實現Consumer接口的accept方法,實現自己對集合元素需要做的自定義操作。比如:下面的代碼是實現集合中字符串轉大寫并打印出來的操作。 ![](https://img.kancloud.cn/c2/41/c2415cdf1368acb1c11a0d9c7888cef8_572x374.png) #### 1.2. Map.forEach() Map.forEach()方法對map中的每一個entry執行特定的操作,直到所有map的entry被處理完成或者拋出異常。 ![](https://img.kancloud.cn/e5/fa/e5fa75e1e848d00d76447bcdfba2651e_606x337.png) 使用Map.forEach() 方法 ![](https://img.kancloud.cn/f6/bf/f6bff1608225b3d5923e8986efb012c0_520x325.png) 與List等集合類遍歷類似,我們可以自定義一個biconsumer action去處理key-value鍵值對. ![](https://img.kancloud.cn/63/0f/630fee49958b3af7322278dbf8c4b71d_435x307.png) Program output. ~~~ Key is : A Value is : 1 Key is : B Value is : 2 Key is : C Value is : 3 ~~~ ## 2. 使用forEach遍歷 List的例子 下面的代碼使用forEach遍歷 List中的所有偶數。 ![](https://img.kancloud.cn/71/ef/71ef84531ebb29008d25f61806223db5_476x180.png) 輸出: ~~~ 2 4 ~~~ ## 3. 使用forEach 遍歷 Map We already saw above program to iterate over all entries of a[HashMap](https://howtodoinjava.com/java-hashmap/)and perform an action. We can also iterate over map keys and values and perform any action on all elements. Java 8 forEach map entries ![](https://img.kancloud.cn/bd/9a/bd9a0e0508cac02e9b5c0196d193ed9a_590x430.png) Program output. ~~~ A=1 B=2 C=3 A B C 1 2 3 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看