<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 收集器`teeing()`方法示例 > 原文: [https://howtodoinjava.com/java12/collectors-teeing-example/](https://howtodoinjava.com/java12/collectors-teeing-example/) 了解`Collectors.teeing()`方法(在 Java 12 中添加),方法語法以及如何在 Java 的各種用例中應用`teeing()`方法。 ## 1\. `teeing()`的目的 這是`java.util.stream.Collectors`接口的新[靜態方法](https://howtodoinjava.com/java/basics/java-static-keyword/),它允許使用兩個獨立的收集器進行收集,然后使用提供的`BiFunction`合并其結果。 傳遞給結果收集器的每個元素都由兩個下游收集器處理,然后使用指定的合并函數將其結果合并為最終結果。 請注意,此函數有助于一步完成特定任務。 如果不使用`teeing()`函數,我們已經可以分兩步執行給定的任務。 它只是一個幫助函數,可以幫助減少冗長程度。 ## 2\. 語法 ```java /** * downstream1 - the first downstream collector * downstream2 - the second downstream collector * merger - the function which merges two results into the single one * returns - a Collector which aggregates the results of two supplied collectors. */ public static Collector teeing? (Collector downstream1, Collector downstream2, BiFunction merger); ``` ## 3\. 使用`teeing()`查找薪水最高和最低的員工 在此`Collectors.teeing()`示例中,我們有一個雇員列表。 我們要一步找到最高薪的員工和最低薪的員工。 以下 Java 程序執行[查找最大和最小](https://howtodoinjava.com/java8/stream-max-min-examples/)操作,然后將兩個項目收集到`Map`中。 ```java import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.HashMap; import java.util.Optional; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Employee> employeeList = Arrays.asList( new Employee(1, "A", 100), new Employee(2, "B", 200), new Employee(3, "C", 300), new Employee(4, "D", 400)); HashMap<String, Employee> result = employeeList.stream().collect( Collectors.teeing( Collectors.maxBy(Comparator.comparing(Employee::getSalary)), Collectors.minBy(Comparator.comparing(Employee::getSalary)), (e1, e2) -> { HashMap<String, Employee> map = new HashMap(); map.put("MAX", e1.get()); map.put("MIN", e2.get()); return map; } )); System.out.println(result); } } ``` 程序輸出。 ```java C:\BAML\DFCCUI\installs\jdk-12.0.1\bin>java Main.java { MIN=Employee [id=1, name=A, salary=100.0], MAX=Employee [id=4, name=D, salary=400.0] } ``` 這里的`Employee`類是這樣的。 ```java class Employee { private long id; private String name; private double salary; public Employee(long id, String name, double salary) { super(); this.id = id; this.name = name; this.salary = salary; } //Getters and setters @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]"; } } ``` ## 4\. 使用`teeing()`過濾項目并計數 在此示例中,我們將使用同一組員工。 在這里,我們將找到所有薪水高于 200 的員工,然后我們還將計算這些員工的數量。 ```java import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.HashMap; import java.util.Optional; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Employee> employeeList = Arrays.asList( new Employee(1, "A", 100), new Employee(2, "B", 200), new Employee(3, "C", 300), new Employee(4, "D", 400)); HashMap<String, Object> result = employeeList.stream().collect( Collectors.teeing( Collectors.filtering(e -> e.getSalary() > 200, Collectors.toList()), Collectors.filtering(e -> e.getSalary() > 200, Collectors.counting()), (list, count) -> { HashMap<String, Object> map = new HashMap(); map.put("list", list); map.put("count", count); return map; } )); System.out.println(result); } } ``` 程序輸出: ```java C:\BAML\DFCCUI\installs\jdk-12.0.1\bin>java Main.java { count=2, list=[Employee [id=3, name=C, salary=300.0], Employee [id=4, name=D, salary=400.0]] } ``` ## 5\. 總結 上面的`Collectors.teeing()`方法示例非常簡單,為便于基本理解而編寫。 您需要使用非常適合您自己需要的函數。 只需記住,當您需要執行兩次流操作并在兩個不同的收集器中收集結果時,請考慮使用`teeing()`方法。 它并不總是適合用例,但適合時可能會有用。 學習愉快! 參考: [Java 文檔](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/stream/Collectors.html#teeing(java.util.stream.Collector,java.util.stream.Collector,java.util.function.BiFunction))
                  <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>

                              哎呀哎呀视频在线观看