<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 8 `forEach` > 原文: [https://howtodoinjava.com/java8/foreach-method-example/](https://howtodoinjava.com/java8/foreach-method-example/) Java `forEach`是一種工具方法,可迭代諸如(列表,集合或映射)和[流](https://howtodoinjava.com/java8/java-streams-by-examples/)之類的集合,并對它的每個元素執行特定操作。 ## 1\. Java 8 `forEach` 方法 #### 1.1 `Iterable.forEach()` 下面的代碼片段顯示了[`Iterable`](https://howtodoinjava.com/java/collections/java-iterator/)接口中[`forEach`](https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-)的默認實現。 它使`Iterable.forEach()`方法**可用于除`Map`** 之外的所有集合類。 下一節將討論`Map`中的 *`forEach()`方法。* ```java default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } ``` 上面的方法*對`Iterable`的每個元素*執行給定的操作,直到所有元素都已處理或該操作引發異常。 `action`表示接受單個輸入參數且不返回結果的操作。 它是`Consumer`接口的實例。 ```java List<String> names = Arrays.asList("Alex", "Brian", "Charles"); names.forEach(System.out::println); //Console output Alex Brian Charles ``` 可以使用此簡單語法創建**自定義消費者操作**。 這里`Object`類型應替換為集合或流中元素的類型。 ```java List<String> names = Arrays.asList("Alex", "Brian", "Charles"); Consumer<String> makeUpperCase = new Consumer<String>() { @Override public void accept(String t) { System.out.println(t.toUpperCase()); } }; names.forEach(makeUpperCase); //Console output ALEX BRIAN CHARLES ``` #### 1.2 `Map.forEach()` 此方法對此映射中的每個條目執行給定的 [`BiConsumer`](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html) 操作,直到已處理所有條目或該操作引發異常。 ```java default void forEach(BiConsumer<? super K, ? super V> action) { Objects.requireNonNull(action); for (Map.Entry<K, V> entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } } ``` ```java Map<String, String> map = new HashMap<String, String>(); map.put("A", "Alex"); map.put("B", "Brian"); map.put("C", "Charles"); map.forEach((k, v) -> System.out.println("Key = " + k + ", Value = " + v)); //Console Output Key = A, Value = Alex Key = B, Value = Brian Key = C, Value = Charles ``` 與`List`示例類似,我們可以創建*自定義雙消費者操作*,該操作從映射中獲取鍵值對,然后一次處理每個條目。 ```java BiConsumer<String, Integer> action = (a, b) -> { System.out.println("Key is : " + a); System.out.println("Value is : " + b); }; Map<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); map.forEach(action); ``` 程序輸出。 ```java Key is : A Value is : 1 Key is : B Value is : 2 Key is : C Value is : 3 ``` ## 2\. 使用`List`的`forEach`示例 Java 程序迭代流的所有元素并執行操作。 在此示例中,我們將打印所有偶數。 ```java List<Integer> numberList = Arrays.asList(1,2,3,4,5); Consumer<Integer> action = System.out::println; numberList.stream() .filter(n -> n%2 == 0) .forEach( action ); ``` 程序輸出: ```java 2 4 ``` ## 3\. 使用`Map`的`forEach`示例 我們已經看到上面的程序遍歷[`HashMap`](https://howtodoinjava.com/java-hashmap/)的所有條目并執行操作。 我們還可以遍歷映射鍵和值,并對所有元素執行任何操作。 ```java HashMap<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); //1\. Map entries Consumer<Map.Entry<String, Integer>> action = System.out::println; map.entrySet().forEach(action); //2\. Map keys Consumer<String> actionOnKeys = System.out::println; map.keySet().forEach(actionOnKeys); //3\. Map values Consumer<Integer> actionOnValues = System.out::println; map.values().forEach(actionOnValues); ``` 程序輸出: ```java 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>

                              哎呀哎呀视频在线观看