<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之旅 廣告
                - stream流的生成 ``` // 1,校驗通過Collection 系列集合提供的stream()或者paralleStream() ArrayList list = new ArrayList(); Stream stream = list.stream(); // 2.通過Arrays的靜態方法stream()獲取數組流 String[] strs = new String[10]; Stream<String> stream2 = Arrays.stream(strs); // 3.通過Stream類中的靜態方法of Stream<String> stream3 = Stream.of("a", "b", "c"); // 4.創建無限流 / 迭代 Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 1); // 生成 Stream<Double> stream5 = Stream.generate(()-> Math.random()); ``` - Stream的中間操作: ``` 1. stream流的篩選過濾去重 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小軍", 3),new Employee("小李", 2)); stream.filter(x -> x.getEmployId() > 1) .limit(2) .skip(1) // 需要流中的元素重寫hashCode和equals方法 .distinct() .forEach(System.out::println); --------------------------------------------------------------------------------------- 2. 生成新的流,通過map映射 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小軍", 3),new Employee("小李", 2)); stream.map(e-> e.getEmployeeName()) .forEach(System.out::println); --------------------------------------------------------------------------------------- 3. stream流自然排序 定制排序 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小軍", 3),new Employee("小李", 2)); stream.sorted((emp1, emp2) -> { if(emp1.getEmployId() > emp2.getEmployId()) { return -1; }else if(emp1.getEmployId() == emp2.getEmployId()) { return 0; }else { return 1; } }) .forEach(System.out::println); ``` - Stream的終止操作: ``` /** * 終止操作 * 查找和匹配 * allMatch-檢查是否匹配所有元素 * anyMatch-檢查是否至少匹配一個元素 * noneMatch-檢查是否沒有匹配所有元素 * findFirst-返回第一個元素 * findAny-返回當前流中的任意元素 * count-返回流中元素的總個數 * max-返回流中最大值 * min-返回流中最小值 */ List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3)); // 注意,當集合元素為0時返回true boolean allMatch = asList.stream().allMatch(emp-> emp.getEmployId() >= 1); System.out.println(allMatch); boolean anyMatch = asList.stream().anyMatch(emp-> emp.getEmployId() > 2); System.out.println(anyMatch); boolean noneMatch = asList.stream().noneMatch(emp-> emp.getEmployId() > 4); System.out.println(noneMatch); Optional<Employee> findFirst = asList.stream().findFirst(); System.out.println(findFirst.get()); Optional<Employee> findAny = asList.parallelStream().findAny(); System.out.println(findAny.get()); long count = asList.stream().count(); System.out.println(count); Optional<Employee> max = asList.stream().max((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId())); System.out.println(max.get()); Optional<Employee> min = asList.stream().min((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId())); System.out.println(min.get()); --------------------------------------------------------------------------------------------------------------------- // reduce操作: reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以將流中元素反復結合起來,得到一個值 List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); Integer count2 = list.stream() .reduce(0, Integer::sum); System.out.println(count2); Optional<Integer> sum = list.stream() .reduce(Integer::sum); System.out.println(sum.get()); --------------------------------------------------------------------------------------------------------------------- // collect操作:Collect-將流轉換為其他形式,接收一個Collection接口的實現,用于給Stream中元素做匯總的方法 List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3)); List<Integer> collect = asList.stream().map(e->e.getEmployId()).collect(Collectors.toList()); System.out.println(collect); ``` ***** 原文鏈接:https://blog.csdn.net/qq_29411737/article/details/80835658
                  <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>

                              哎呀哎呀视频在线观看