Java Stream流使用案例深入詳解
前言
項(xiàng)目一直在用流,但是用的也是一知半解,所以在這里深入學(xué)習(xí)一下
通常用到流會(huì)設(shè)計(jì)到Java8的幾個(gè)新知識(shí),下邊我回粗略的講解下這幾個(gè)知識(shí),再了解認(rèn)知后學(xué)習(xí)stream或者用到的時(shí)候更得心應(yīng)手
- Lambda表達(dá)式
- 方法引用
- Option
- stream
1. Lambda
建議先了解函數(shù)式接口
1.1 語法
parameter -> expression body;
1.2 沒參數(shù)只有一條語句或者多條語句
()->System.out.prilt("baicaizhi"); ()->{ System.out.prilt("baicaizhi1"); System.out.prilt("baicaizhi1"); }
1.3 一個(gè)參數(shù)只有一條語句或者多條語句
a->System.out.println(a); a->{ System.out.println(a); System.out.println(a); }
1.4 多個(gè)參數(shù)只有一條語句或者多條語句
(a,b)->a+b; (a,b)->{ int c = a+b; System.out.println(c); }
- 可選的參數(shù)類型聲明 : 無需聲明參數(shù)的類型。編譯器可以從參數(shù)的值推斷出相同的值。
- 可選的參數(shù)周圍的小括號(hào) () : 如果只有一個(gè)參數(shù),可以忽略參數(shù)周圍的小括號(hào)。但如果有多個(gè)參數(shù),則必須添加小括號(hào)。
- 可選的大括號(hào) {} : 如果 Lambda 表達(dá)式只包含一條語句,那么可以省略大括號(hào)。但如果有多條語句,則必須添加大括號(hào)。
- 可選的 return 關(guān)鍵字 : 如果 Lambda 表達(dá)式只有一條語句,那么編譯器會(huì)自動(dòng) return 該語句最后的結(jié)果。但如果顯式使用了 return 語句,則必須添加大括號(hào) {} ,哪怕只有一條語句。
2.方法引用
使用:(靜態(tài)方法或者new的時(shí)候用)
Test::new Test::getName
3.Option
接口名稱 | 簡要作用描述 |
---|---|
Optional empty() | 構(gòu)建一個(gè)空的Optional 對象 |
Optional of(T value) | 構(gòu)建一個(gè)非空的Optional 對象,如果為空則報(bào)錯(cuò)! |
Optional ofNullable(T value) | 構(gòu)建一個(gè)Optional 對象,允許為空! |
T get() | 獲取一個(gè)泛型的對象值,如果值為空,則報(bào)錯(cuò) |
boolean isPresent() | 判空,如果不為null 則為 true |
boolean isEmpty() | 判空,如果為null 則為 true |
ifPresent(Consumer) | 傳遞一個(gè)接口函數(shù)對,當(dāng)數(shù)據(jù)不為空的時(shí)候執(zhí)行這個(gè)函數(shù) |
ifPresentOrElse(Consumer, Runnable) | 兩個(gè)參數(shù), 第一個(gè)是不為空的時(shí)候執(zhí)行的,第二個(gè)是為空的時(shí)候執(zhí)行的。都是接口函數(shù)。 |
Optional filter | 對對象的一個(gè)過濾 |
Optional map(Function) | 轉(zhuǎn)換方法 |
Optional flatMap(Function) | 轉(zhuǎn)換方法,常用與多層轉(zhuǎn)換一層 |
Optional or(Supplier) | 當(dāng)?shù)玫綄ο鬄榭盏臅r(shí)候根據(jù)接口函數(shù)創(chuàng)建一個(gè)新的Optional對象 |
T orElse(T) | 當(dāng)?shù)玫綄ο鬄榭盏臅r(shí)候獲取一個(gè)指定泛型對象 |
T orElseThrow() | 不為空 返回對象,為空 則NoSuchElementException |
T orElseThrow(Supplier) | 不為空 返回對象,為空 則指定異常 |
4.Stream
4.1Stream概述
4.1.1什么是steam
Stream將要處理的元素集合看作一種流,在流的過程中,借助Stream API對流中的元素進(jìn)行操作,比如:篩選、排序、聚合等。
4.1.2Stream可以由數(shù)組和集合創(chuàng)建,對流的操作分為倆類
4.1.2.1中間操作
每次返回一個(gè)新的流,可以有多個(gè)。
4.1.2.2終端操作
終端操作,每個(gè)流只能進(jìn)行一次終端操作,終端操作結(jié)束后流無法再次使用。終端操作會(huì)產(chǎn)生一個(gè)新的集合或值
4.1.3特性
1.stream不存儲(chǔ)數(shù)據(jù),而是按照特定的規(guī)則對數(shù)據(jù)進(jìn)行計(jì)算,一般會(huì)輸出結(jié)果。
2.stream不會(huì)改變數(shù)據(jù)源,通常情況下會(huì)產(chǎn)生一個(gè)新的集合或一個(gè)值。
3.stream具有延遲執(zhí)行特性,只有調(diào)用終端操作時(shí),中間操作才會(huì)執(zhí)行。
3.2Stream的創(chuàng)建
3.2.1通過 java.util.Collection.stream() 方法用集合創(chuàng)建流
List<String> list = Arrays.asList("a","b","c"); //創(chuàng)建順序流 Stream<String> stream = list.stream(); //創(chuàng)建并發(fā)流 Stream<String> stringStream = list.parallelStream();
3.2.2使用java.util.Arrays.stream(T[] array)方法用數(shù)組創(chuàng)建流
//數(shù)組創(chuàng)建流 int[] array = {1,2,3}; IntStream stream1 = Arrays.stream(array);
3.2.3使用Stream的靜態(tài)方法:of()、iterate()、generate()
//stream靜態(tài)方法創(chuàng)建流 Stream<Integer> integerStream = Stream.of(1, 2); Stream<Integer> iterate = Stream.iterate(0, x -> x = 3); Stream<Double> limit = Stream.generate(Math::random).limit(3);
3.2.4順序流轉(zhuǎn)換成并發(fā)流
//順序流轉(zhuǎn)換成并發(fā)流 Optional<String> first = list.stream().parallel().filter(x -> x > 6).findFirst();
4.3 使用
使用前先了解Optional
4.3.1 數(shù)據(jù)準(zhǔn)備
class Person { private String name; // 姓名 private int salary; // 薪資 private int age; // 年齡 private String sex; //性別 private String area; // 地區(qū) // 構(gòu)造方法 public Person(String name, int salary, int age,String sex,String area) { this.name = name; this.salary = salary; this.age = age; this.sex = sex; this.area = area; } // 省略了get和set,請自行添加 }
4.3.2 使用
4.3.2.1 遍歷/匹配(foreach/find/match)
Stream也是支持類似集合的遍歷和匹配元素的,只是Stream中的元素是以O(shè)ptional類型存在的。Stream的遍歷、匹配非常簡單
List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); // 遍歷輸出符合條件的元素 list.stream().filter(x->x>6).forEach(System.out::println); // 匹配第一個(gè) Optional<Integer> first = list.stream().filter(x -> x > 6).findFirst(); // 匹配任意(適用于并行流) Optional<Integer> any = list.parallelStream().filter(x -> x > 6).findAny(); // 是否包含符合特定條件的元素 boolean b = list.stream().anyMatch(x -> x < 6); System.out.println("匹配第一個(gè)值"+first.get()); System.out.println("匹配任意值"+any.get()); System.out.println("是否存在大于6的值"+b);
4.3.2.2 篩選(filter)
篩選,是按照一定的規(guī)則校驗(yàn)流中的元素,將符合條件的元素提取到新的流中的操作
4.3.2.3 聚合(max/min/count)
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); //篩選出Integer集合中大于7的元素,并打印出來 list.stream().filter(x->x>7).forEach(System.out::println); //篩選員工中工資高于8000的人,并形成新的集合。 形成新集合依賴collect(收集) List<String> collect = personList.stream().filter(value -> value.getSalary() > 8000).map(Person::getName).collect(Collectors.toList()); System.out.println("工資高于8000"+collect);
4.3.2.4 映射(map/flatMap)
映射,可以將一個(gè)流的元素按照一定的映射規(guī)則映射到另一個(gè)流中。分為map和flatMap:
- map:接收一個(gè)函數(shù)作為參數(shù),該函數(shù)會(huì)被應(yīng)用到每個(gè)元素上,并將其映射成一個(gè)新的元素。
- flatMap:接收一個(gè)函數(shù)作為參數(shù),將流中的每個(gè)值都換成另一個(gè)流,然后把所有流連接成一個(gè)流。
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); //英文字符串?dāng)?shù)組的元素全部改為大寫。整數(shù)數(shù)組每個(gè)元素+3 List<Integer> collect = list.stream().map(x -> x + 3).collect(Collectors.toList()); List<String> collect1 = strList.stream().map(String::toUpperCase).collect(Collectors.toList()); //將員工的薪資全部增加1000 //不改變源集合的方式 List<Person> collect2 = personList.stream().map(person -> { Person person1 = new Person(person.getName(), 0, person.getAge(), person.getSex(), person.getArea()); person1.setSalary(person.getSalary() + 1000); return person1; }).collect(Collectors.toList()); //改變源集合的方式 List<Person> collect3 = personList.stream().map(person -> { person.setSalary(person.getSalary() + 1000); return person; }).collect(Collectors.toList()); //將兩個(gè)字符數(shù)組合并成一個(gè)新的字符數(shù)組 List<String> collect4 = strList.stream().flatMap(s -> { String[] s2 = s.split(","); return Arrays.stream(s2); }).collect(Collectors.toList()); System.out.println("每個(gè)元素大寫:" + collect1); System.out.println("每個(gè)元素+3:" + collect); //注意,執(zhí)行的時(shí)候分開執(zhí)行,否則看不出來效果 System.out.println("一次改動(dòng)前:" + personList.get(0).getName() + "-->" + personList.get(0).getSalary()); System.out.println("一次改動(dòng)后:" + collect2.get(0).getName() + "-->" + collect2.get(0).getSalary()); System.out.println("二次改動(dòng)前:" + personList.get(0).getName() + "-->" + personList.get(0).getSalary()); System.out.println("二次改動(dòng)后:" + collect3.get(0).getName() + "-->" + collect3.get(0).getSalary()); System.out.println("處理前的集合:" + strList); System.out.println("處理后的集合:" + collect4);
4.3.2.5 歸約(reduce)
歸約,也稱縮減,顧名思義,是把一個(gè)流縮減成一個(gè)值,能實(shí)現(xiàn)對集合求和、求乘積和求最值操作
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); // 求Integer集合的元素之和、乘積和最大值 // 求和方式1 Optional<Integer> reduce = list.stream().reduce((x, y) -> x + y); // 求和方式2 Optional<Integer> reduce1 = list.stream().reduce(Integer::sum); // 求和方式3 Integer reduce2 = list.stream().reduce(0, Integer::sum); // 求乘積 Optional<Integer> reduce3 = list.stream().reduce((x, y) -> x * y); // 求最大值方式1 Optional<Integer> reduce4 = list.stream().reduce((x, y) -> x > y ? x : y); // 求最大值寫法2 Integer reduce5 = list.stream().reduce(1, Integer::max); // 求所有員工的工資之和和最高工資 // 求和方式1 Optional<Integer> reduce7 = personList.stream().map(Person::getSalary).reduce(Integer::sum); // 求和方式2 Integer reduce6 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(),(sum1,sum2)->sum1+sum2); // 求和方式3 Integer reduce8 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(), Integer::sum); // 求最高工資方式1: Integer reduce9 = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(),Integer::max); // 求最高工資方式2: Integer reduce10 = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(), (max1, max2) -> max1 > max2 ? max1 : max2); System.out.println("list求和:" + reduce.get() + "," + reduce1.get() + "," + reduce2); System.out.println("list求積:" + reduce3.get()); System.out.println("list求最大值:" + reduce4.get() + "," + reduce5); System.out.println("工資之和:" + reduce7.get() + "," + reduce6 + "," + reduce8); System.out.println("最高工資:" + reduce9 + "," + reduce10);
4.3.2.6 收集(collect)
解釋
- collect,收集,可以說是內(nèi)容最繁多、功能最豐富的部分了。從字面上去理解,就是把一個(gè)流收集起來,最終可以是收集成一個(gè)值也可以收集成一個(gè)新的集合
- collect主要依賴java.util.stream.Collectors類內(nèi)置的靜態(tài)方法
4.3.2.6.1 歸集(toList/toSet/toMap)
因?yàn)榱鞑淮鎯?chǔ)數(shù)據(jù),那么在流中的數(shù)據(jù)完成處理后,需要將流中的數(shù)據(jù)重新歸集到新的集合里。toList、toSet和toMap比較常用,另外還有toCollection、toConcurrentMap等復(fù)雜一些的用法
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); List<Integer> collect = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList()); Set<Integer> collect1 = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet()); Map<String, Person> collect2 = personList.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p)); System.out.println("toList:" + collect); System.out.println("toSet:" + collect1); System.out.println("toMap:" + collect2);
4.3.2.6.2 統(tǒng)計(jì)(count/averaging)
Collectors提供了一系列用于數(shù)據(jù)統(tǒng)計(jì)的靜態(tài)方法
- 計(jì)數(shù):count
- 平均值:averagingInt、averagingLong、averagingDouble
- 最值:maxBy、minBy
- 求和:summingInt、summingLong、summingDouble
- 統(tǒng)計(jì)以上所有:summarizingInt、summarizingLong、summarizingDouble
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); // 統(tǒng)計(jì)員工人數(shù)、平均工資、工資總額、最高工資 // 求總數(shù) Long collect = personList.stream().collect(Collectors.counting()); // 求平均工資 Double collect1 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary)); // 求最高工資 Optional<Integer> collect2 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare)); // 求工資之和 Integer collect3 = personList.stream().collect(Collectors.summingInt(Person::getSalary)); // 一次性統(tǒng)計(jì)所有信息 DoubleSummaryStatistics collect4 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary)); System.out.println("員工總數(shù):" + collect); System.out.println("員工平均工資:" + collect1); System.out.println("員工工資總和:" + collect2.get()); System.out.println("員工工資所有統(tǒng)計(jì):" + collect3);
4.3.2.6.3 分組(partitioningBy/groupingBy)
解釋
- 分區(qū):將stream按條件分為兩個(gè)Map,比如員工按薪資是否高于8000分為兩部分。
- 分組:將集合分為多個(gè)Map,比如員工按性別分組。有單級(jí)分組和多級(jí)分組。
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); //將員工按薪資是否高于8000分為兩部分;將員工按性別和地區(qū)分組 // 將員工按薪資是否高于8000分組 Map<Boolean, List<Person>> collect = personList.stream().collect(Collectors.partitioningBy(person -> person.getSalary() > 8000)); // 將員工按性別分組 Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getSex)); // 將員工先按性別分組,再按地區(qū)分組 Map<String, Map<String, List<Person>>> collect2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea))); System.out.println("員工按薪資是否大于8000分組情況:" + collect); System.out.println("員工按性別分組情況:" + collect1); System.out.println("員工按性別、地區(qū):" + collect2);
4.3.2.6.4 接合(joining)
joining可以將stream中的元素用特定的連接符(沒有的話,則直接連接)連接成一個(gè)字符串。
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); //字符串拼接 String collect = strList.stream().collect(Collectors.joining("-")); //所有員工的名字 String collect1 = personList.stream().map(Person::getName).collect(Collectors.joining(",")); System.out.println("所有員工的姓名:" + collect1); System.out.println("拼接后的字符串:" + collect);
4.3.2.6.5 歸約(reducing)
Collectors類提供的reducing方法,相比于stream本身的reduce方法,增加了對自定義歸約的支持
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); // 每個(gè)員工減去起征點(diǎn)后的薪資之和(這個(gè)例子并不嚴(yán)謹(jǐn),但一時(shí)沒想到好的例子) Integer collect = personList.stream().collect(Collectors.reducing(0, Person::getSalary, (i, j) -> i + j - 5000)); System.out.println("員工扣稅薪資總和:" + collect); // stream的reduce Optional<Integer> reduce = personList.stream().map(Person::getSalary).reduce(Integer::sum); System.out.println("員工扣稅薪資總和:" + reduce.get());
4.3.2.7 排序(sorted)
解釋
- sorted():自然排序,流中元素需實(shí)現(xiàn)Comparable接口
- sorted(Comparator com):Comparator排序器自定義排序
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 11,"male", "New York")); personList.add(new Person("Jack", 7000, 12,"male", "Washington")); personList.add(new Person("Lily", 7800, 13,"female", "Washington")); personList.add(new Person("Anni", 8200, 14,"female", "New York")); personList.add(new Person("Owen", 9500, 15,"male", "New York")); personList.add(new Person("Alisa", 7900, 16,"female", "New York")); List<Integer> list = Arrays.asList(1,2,3,4,7,6,5,8); List<String> strList = Arrays.asList("ad,nm", "adm,mt", "p,ot", "xb,angd", "weou,jgsd"); // 按工資增序排序 List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName) .collect(Collectors.toList()); // 按工資倒序排序 List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()) .map(Person::getName).collect(Collectors.toList()); // 先按工資再按年齡自然排序(從小到大) List<String> newList3 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()) .map(Person::getName).collect(Collectors.toList()); // 先按工資再按年齡自定義排序(從大到?。? List<String> newList4 = personList.stream().sorted((p1, p2) -> { if (p1.getSalary() == p2.getSalary()) { return p2.getAge() - p1.getAge(); } else { return p2.getSalary() - p1.getSalary(); } }).map(Person::getName).collect(Collectors.toList()); System.out.println("按工資自然排序:" + newList); System.out.println("按工資降序排序:" + newList2); System.out.println("先按工資再按年齡自然排序:" + newList3); System.out.println("先按工資再按年齡自定義降序排序:" + newList4);
4.3.2.8 提取/組合(distinct,skip,limit)
流也可以進(jìn)行合并、去重、限制、跳過等操作。
String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "d", "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); // concat:合并兩個(gè)流 distinct:去重 List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList()); // limit:限制從流中獲得前n個(gè)數(shù)據(jù) List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList()); // skip:跳過前n個(gè)數(shù)據(jù) List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList()); System.out.println("流合并:" + newList); System.out.println("limit:" + collect); System.out.println("skip:" + collect2);
到此這篇關(guān)于Java Stream流使用案例深入詳解的文章就介紹到這了,更多相關(guān)Java Stream流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot深入探究@Conditional條件裝配的使用
這篇文章主要為大家介紹了SpringBoot底層注解@Conditional的使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06java中的equals()和toString()方法實(shí)例詳解
這篇文章主要介紹了java中的equals()和toString()方法實(shí)例詳解的相關(guān)資料,這里舉例說明,并附實(shí)例代碼,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼圖片生成
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼圖片生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10maven package后Idea項(xiàng)目中找不到target文件的解決
在Idea中執(zhí)行mavenpackage打包后,target文件不顯示,點(diǎn)擊「ShowinExplore」可以在本地文件夾中查到,解決方法:在Idea的Maven工具窗口中,右鍵點(diǎn)擊項(xiàng)目,選擇Reimport,刷新項(xiàng)目即可2024-11-11